Newbie: Difference between $array[0] and @array[0]

D

darren_uk

I'm completely new to Perl.

I've just read an introduction to arrays (in man perlintro).

It explains that to get a single value from an array, use the scalar prefix
"$"

E.g.,

$array[0]

Later it explains that to get multiple values, use the array prefix "@"

E.g.,

@array[0,1]

Is there any difference between:

$array[0]
and
@array[0]
 
J

Jeff 'japhy' Pinyan

Is there any difference between:

$array[0]
and
@array[0]

Yes. As the docs explain, @foo[$x,$y,$z] is the same as

($foo[$x], $foo[$y], $foo[$z])

SPECIFICALLY, it generates a LIST of array elements. The fact that it is
a list is important.

$size = @array; # the number of elements in @array is put in $size
($first) = @array; # the first element in @array is put in $first

If you write:

$lines[$x] = <FILE>;

you read ONE line from FILE, and put it in $lines[$x]. If you write

@lines[$x] = <FILE>;

you read ALL the (remaining) lines from FILE, and store the first in
$lines[$x].
 
S

Steve Grazzini

darren_uk said:
Is there any difference between:

$array[0]
and
@array[0]

The one-element-slice will give you this warning.

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

And perldiag explains:

(W syntax) You've used an array slice (indicated by @) to select a
single element of an array. Generally it's better to ask for a scalar
value (indicated by $). The difference is that $foo[&bar] always
behaves like a scalar, both when assigning to it and when evaluating its
argument, while @foo[&bar] behaves like a list when you assign to it,
and provides a list context to its subscript, which can do weird things
if you're expecting only one subscript.

On the other hand, if you were actually hoping to treat the array
element as a list, you need to look into how references work, because
Perl will not magically convert between scalars and lists for you. See
perlref.

It's an odd explanation, since "@foo[bar()]" won't actually generate the
warning... but the difference between something like "@x[0] = localtime"
and "$x[0] = localtime" is important.
 
D

Dave Oswald

darren_uk said:
Is there any difference between:

$array[0]
and
@array[0]

The first equates to the single scalar element whos value is stored in
$array[0]. The second equates to a list that holds the single value
contained in $array[0].

Consider the following examples:

use strict;

sub context {
if ( wantarray ) {
print "List context.\n";
return 1;
} else {
print "Scalar context.\n";
return 0;
}
}

my @array = qw/Bart Homer Marge Maggy Lisa/;

@array[context()];
$array[context()];
@array[0] = context();
$array[0] = context();

The output will be:
List context.
Scalar context.
List context.
Scalar context.

I leave it to future lessons for you to see the significance and
ramifications of the distinction between list and scalar context.

The idea is that @array[......] is an array slice. An array slice is a list
of values, even if that list is only one value long. $array is an array
element, which is a scalar that contains some value (or no value). Always,
you should use array slices when you need a slice, and array indexing when
you need a scalar.
 
T

Tad McClellan

I've just read an introduction to arrays (in man perlintro).


See also the "Context" section in

perldoc perldata

It explains that to get a single value from an array, use the scalar prefix
"$"
$array[0]


Right.

Dollar sign means: "one thing". (ie. a scalar)

If you use that on the LHS of an assignment, the RHS will
get _scalar_ context.

Later it explains that to get multiple values, use the array prefix "@"
@array[0,1]


Right.

At sign means: "(potentially) many things". (ie. a list)

If you use that on the LHS of an assignment, the RHS will
get _list_ context.

Is there any difference between:

$array[0]
and
@array[0]


Yes sometimes.

No sometimes.

The question cannot be answered without seeing how they are
used in the surrounding code.

If they are used in a place where context matters, then
yes, they are different.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top