Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, what is the difference between accessing an array element using @a[$i] as opposed to using $a[$i]?

Tags:

syntax

perl

Basic syntax tutorials I followed do not make this clear:

Is there any practical/philosophical/context-dependent/tricky difference between accessing an array using the former or latter subscript notation?

$ perl -le 'my @a = qw(io tu egli); print $a[1], @a[1]'

The output seems to be the same in both cases.

like image 898
Robottinosino Avatar asked Mar 31 '13 03:03

Robottinosino


3 Answers

$a[...]   # array element

returns the one element identified by the index expression, and

@a[...]   # array slice

returns all the elements identified by the index expression.

As such,

  • You should use $a[EXPR] when you mean to access a single element in order to convey this information to the reader. In fact, you can get a warning if you don't.
  • You should use @a[LIST] when you mean to access many elements or a variable number of elements.

But that's not the end of the story. You asked for practical and tricky (subtle?) differences, and there's one noone mentioned yet: The index expression for an array element is evaluated in scalar context, while the index expression for an array slice is evaluated in list context.

sub f { return @_; }

$a[ f(4,5,6) ]     # Same as $a[3]
@a[ f(4,5,6) ]     # Same as $a[4],$a[5],$a[6]
like image 200
ikegami Avatar answered Oct 12 '22 06:10

ikegami


If you turn on warnings (which you always should) you would see this:

Scalar value @a[0] better written as $a[0]

when you use @a[1].

The @ sigil means "give me a list of something." When used with an array subscript, it retrieves a slice of the array. For example, @foo[0..3] retrieves the first four items in the array @foo.

When you write @a[1], you're asking for a one-element slice from @a. That's perfectly OK, but it's much clearer to ask for a single value, $a[1], instead. So much so that Perl will warn you if you do it the first way.

like image 41
friedo Avatar answered Oct 12 '22 06:10

friedo


The first yields a scalar variable while the second gives you an array slice .... Very different animals!!

like image 28
ennuikiller Avatar answered Oct 12 '22 06:10

ennuikiller