From: Jerry Cloe on
Having an array:

@mylist=("apples", "oranges", "bananas");

And refering to a particular element, which syntax is correct? (or are both
correct)?

print "Item 2 is $mylist[1]\n";
or
print "Item 2 is @mylist[1]\n";

Both seem to work, and looking at other code, I see it done both ways.


From: Simon Taylor on
Hello Jerry,
:
> Having an array:
>
> @mylist=("apples", "oranges", "bananas");
>
> And refering to a particular element, which syntax is correct? (or are both
> correct)?
>
> print "Item 2 is $mylist[1]\n";
> or
> print "Item 2 is @mylist[1]\n";
>
> Both seem to work, and looking at other code, I see it done both ways.

The first is a scalar value, the second is an array slice.

This is a frequently asked question. So you can use the "-q" option of
the perldoc command to dig it up:

Run:

perldoc -q array

and look for the section:

"What is the difference between $array[1] and @array[1]?"



Regards,

Simon Taylor
--
www.perlmeme.org
From: John W. Krahn on
Jerry Cloe wrote:
> Having an array:
>
> @mylist=("apples", "oranges", "bananas");
>
> And refering to a particular element, which syntax is correct? (or are both
> correct)?
>
> print "Item 2 is $mylist[1]\n";
> or
> print "Item 2 is @mylist[1]\n";
>
> Both seem to work, and looking at other code, I see it done both ways.

Put the statement:

use warnings;

at the top of your program and perl will tell you which is correct.


John
--
use Perl;
program
fulfillment
From: A. Sinan Unur on
"Jerry Cloe" <tom(a)staff.showmeisp.net> wrote in
news:ZM9Re.19164$mb4.2853(a)tornado.rdc-kc.rr.com:

> Having an array:
>
> @mylist=("apples", "oranges", "bananas");

Easier to type:

my @mylist = qw(apples oranges bananas);


> And refering to a particular element, which syntax is correct? (or
> are both correct)?
>
> print "Item 2 is $mylist[1]\n";

This is correct.

> or
> print "Item 2 is @mylist[1]\n";

That is an array slice, probably not what you intended.

> Both seem to work,

You don't have warnings enabled. Have you read the posting guidelines
for this group yet?

Sinan

--
A. Sinan Unur <1usa(a)llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
From: John Bokma on
"A. Sinan Unur" <1usa(a)llenroc.ude.invalid> wrote:

> You don't have warnings enabled. Have you read the posting guidelines
> for this group yet?

I was just wondering, if postings that don't follow the posting guidelines
are just ignored, especially by the regulars *and* together with the FAQ
xx.yy (or maybe instead of) a message is posted:

Subject: Why is your message not answered?

With a link to posting guidelines
(and to the FAQ)?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 |  Next  |  Last
Pages: 1 2
Prev: got time out in ftp
Next: string compare