But what's the "-1" for? Why not just "$b[$a]" instead? That's what I'm confused
about.
-----Original Message-----
From: Jenda Krynicky [mailto:[EMAIL PROTECTED]
Sent: Friday, June 20, 2003 12:19 PM
To: [EMAIL PROTECTED]
Subject: Re: Array Question (Learning Perl/Win32 Chapter Test
Question)
From: "Anthony Beaman" <[EMAIL PROTECTED]>
> 2. Write a program that reads a number and then a list of strings
(all
> on separate lines), and then prints one of the lines from the list as
> selected by the number.
>
> One way to do this is:
> print "Enter the line number: "; chomp($a = <STDIN>);
> print "Enter the lines, end with ^Z:\n"; @b = <STDIN>;
> print "Answer: $b[$a-1]";
> The first line prompts for a number, reads it from standard input,
and
> removes that pesky newline. The second line asks for a list of
> strings, then uses the <STDIN> operator in a list context to read all
> of the lines until end-of-file into an array variable. The final
> statement prints the answer, using an array reference to select the
> proper line. Note that we don't have to add a newline to the end of
> this string, because the line selected from the @b array still has
its
> newline ending. You'll need to type CTRL-Z at the console to indicate
> an end-of-file.
>
> I'm confused on the [$a-1] part of the last line. Why not $b[$a]
> instead?
Because arrays are indexed from 0. So the first line entered is in
$b[0] :-)
Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]