Home > Archive > PERL Miscellaneous > August 2005 > array question (newbie)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
array question (newbie)
|
|
| Jerry Cloe 2005-08-31, 3:58 am |
| 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.
| |
| Simon Taylor 2005-08-31, 3:58 am |
| 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
| |
| John W. Krahn 2005-08-31, 3:58 am |
| 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
| |
| A. Sinan Unur 2005-08-31, 3:58 am |
| "Jerry Cloe" <tom@staff.showmeisp.net> wrote in
news:ZM9Re.19164$mb4.2853@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@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/c...guidelines.html
| |
| John Bokma 2005-08-31, 3:58 am |
| "A. Sinan Unur" <1usa@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
| |
| A. Sinan Unur 2005-08-31, 7:56 am |
| John Bokma <john@castleamber.com> wrote in
news:Xns96C2F158BB4CFcastleamber@130.133.1.4:
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>
>
> 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)?
Even if we ignore messages, I am sure someone will reply. If, say the OP
in this thread, had to choose between reading a separate "Why is your
message not answered?" thread as opposed to those replies, I have a
feeling they'll choose the response, and ignore the pointers.
Sinan
--
A. Sinan Unur <1usa@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/c...guidelines.html
| |
| Paul Lalli 2005-08-31, 6:57 pm |
| 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.
That "other" code is almost assuredly wrong. Please do see the
responses by other posters. In the meantime, however, some more
information:
both $foo[1] and @foo[1] will most likely Do What You Want - When
you're READING them only. If you are writing to the two values, you
are imposing two different contexts on whatever you're evaluating. The
FAQ that others have referred you to gives as an example reading the
output of an external program through back-ticks. Here's a possibly
more simple-to-understand example:
my @sizes;
my (@foo, @bar);
@foo = (4..12);
@bar = qw/a b c d e f g h i j/;
$sizes[0] = @foo; # correct, sets $size[0] to size of @foo, which is 9
@sizes[1] = @bar; # OOPS! sets $size[1] to the *first element* of
foo,
# which is 'a';
The reason for this is that $size[0] is a scalar value, and so scalar
context is imposed on the array @foo. An array in scalar context
returns its size.
@size[1], on the other hand, is a list containing a single value.
Assigning to a list imposes list context. This line is equivalent to
having done:
my ($size) = @bar;
rather than
my $size = @bar;
When you assign one list to another, the values are copied over
directly. The first value on the right gets assigned to the first
variable on the left, the second on the right to the second on the
left, etc.
The reason Perl warns you when you use @size[1] even in a read (when it
would actually probably not hurt your code) is that getting into the
habbit of writing @size[1] when you mean $size[1] will come back to
bite you, when you attempt to use it in a write, as I did above.
I hope you find this explanation helpful...
Paul Lalli
| |
| John Bokma 2005-08-31, 6:57 pm |
| "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> John Bokma <john@castleamber.com> wrote in
> news:Xns96C2F158BB4CFcastleamber@130.133.1.4:
>
guidelines[color=darkred]
together[color=darkred]
>
> Even if we ignore messages, I am sure someone will reply.
Most replies are by regulars which write the same over and over again
(use strict, etc.). I am sure most of them can be convinced to stop
replying.
> If, say the OP
> in this thread, had to choose between reading a separate "Why is your
> message not answered?" thread as opposed to those replies, I have a
> feeling they'll choose the response, and ignore the pointers.
Most even ignore the response as well, as you probably must have noticed
on many occassions. If most regulars stop replying to posts that don't
follow the posting guidelines, or are not about Perl, a lot of noise
might be gone.
I am going to try it :-D.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
| |
| A. Sinan Unur 2005-08-31, 6:57 pm |
| John Bokma <john@castleamber.com> wrote in
news:Xns96C38803D3FB0castleamber@130.133.1.4:
> If most regulars stop replying to posts that don't follow
> the posting guidelines, or are not about Perl, a lot of
> noise might be gone.
I agree it is worth a shot.
> I am going to try it :-D.
I'll try to follow your lead.
Sinan
--
A. Sinan Unur <1usa@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/c...guidelines.html
| |
| John W. Krahn 2005-08-31, 9:56 pm |
| A. Sinan Unur wrote:
> John Bokma <john@castleamber.com> wrote in
> news:Xns96C38803D3FB0castleamber@130.133.1.4:
>
>
> I agree it is worth a shot.
>
>
> I'll try to follow your lead.
Ah, finally a leader we can all follow. ;-)
John
--
use Perl;
program
fulfillment
|
|
|
|
|