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

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

am 01.12.2007 15:03:03 von PerlFAQ Server

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

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

The former is a scalar value; the latter an array slice, making it a
list with one (scalar) value. You should use $ when you want a scalar
value (most of the time) and @ when you want a list with one scalar
value in it (very, very rarely; nearly never, in fact).

Sometimes it doesn't make a difference, but sometimes it does. For
example, compare:

$good[0] = `some program that outputs several lines`;

with

@bad[0] = `same program that outputs several lines`;

The "use warnings" pragma and the -w flag will warn you about these
matters.



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.