Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Explaining why to use $array[0] instead of @array[0]

2 views
Skip to first unread message

DavidB

unread,
Oct 21, 2008, 4:32:02 PM10/21/08
to perl-t...@perl.org
Hi, I can think of some reasons we use $ instead of @ for array
indices (in Perl5):

1. because we are referring to a scalar, not an array slice, and
it's misleading to use @
2. because it's common usage and all professional code uses it
3. because @array[0] may in some selected cases be treated as a
list, for example @array[0] = @array2 (copies first element) vs.
$array[0] = @array2 (copies number of elements)

however, aside from the extremely limited-application code example in
3. above, I was unable to come up with a practical justification for
using @.

I'm talking demonstrable reason, not any "just because it's proper"
reasons like 1 and 2 above. I can talk their ears off telling them
it's the right thing to do, but I'd prefer to have some practical
reason.

thanks!

David


Jacinta Richardson

unread,
Oct 21, 2008, 8:23:37 PM10/21/08
to DavidB, perl-t...@perl.org
DavidB wrote:

> I'm talking demonstrable reason, not any "just because it's proper"
> reasons like 1 and 2 above. I can talk their ears off telling them
> it's the right thing to do, but I'd prefer to have some practical
> reason.

We provide some of these in our Perl Tip: Common problems with slices
http://perltraining.com.au/tips/2005-09-05.html

Some examples from that tip:

@data[1] = <STDIN>; # Almost certainly a mistake

my @array = ( 'a' .. 'e' );
@array[@array] = 'f'; # Does not generate a warning!

All the best,

J


--
("`-''-/").___..--''"`-._ | Jacinta Richardson |
`6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia |
(_Y_.)' ._ ) `._ `. ``-..-' | +61 3 9354 6001 |
_..`--'_..-_/ /--'_.' ,' | con...@perltraining.com.au |
(il),-'' (li),' ((!.-' | www.perltraining.com.au |

Paul Fenwick

unread,
Oct 21, 2008, 8:25:22 PM10/21/08
to DavidB, perl-t...@perl.org
G'day David,

DavidB wrote:
> Hi, I can think of some reasons we use $ instead of @ for array
> indices (in Perl5):

My usual explanation goes a little like this:

In Perl, $ is pronounced 'the' or 'a' or 'this' (ie, singular):

$name # The name
$friend[0] # The first friend
$colour_of{grass} # The colour of grass
$ref->{$key} # The value $key using this reference.

On the other hand, @ means 'these' or 'those':

@friend # These friends
@friend[0..3] # These four friends

When we write an @ sign when trying to retrieve a single element, we're
really asking Perl for a list of elements. If what we really want back is
just a single element, then Perl can get confused.

I don't actually show the cases where Perl gets it wrong, instead I mention
that Perl:

(a) Will warn you if it figures out that you wanted a scalar when
you asked for a list.

(b) Will *not* warn you when it decides you actually meant a list
of one element. This means debugging your code will be a
painful experience, because Perl won't help you.

Hence the result of using @ incorrectly is almost always a warning or a
hard-to-find bug, both of which are extremely undesirable.

Cheerio,

Paul

--
Paul Fenwick <p...@perltraining.com.au> | http://perltraining.com.au/
Director of Training | Ph: +61 3 9354 6001
Perl Training Australia | Fax: +61 3 9354 2681

0 new messages