On Tue, Sep 16, 2014 at 4:22 AM, Uday Vernekar <vernekaru...@gmail.com>
wrote:

> Confusion on array and list. can anybody explain me the difference between
> list and arrays.
>
> my @xyz = ( 4, 5, 6 );
>
> The right-hand side of the equals sign is a list.here I assign that list
> to the variable @xyz.
> its an array,list can be assigned to an array.
>
> on similiar lines
> we can assign Lists to hashes
> my %zzz = ( a => 42, b => 43, b => 44 );
>


Not sure exactly what your confusion is, you are correct. Couple notes, one
is, the hash assigned list uses the "super comma" ("=>") which is actually
just a fancy comma; it quotes the left hand side for you, so:
( a => 42, b => 43, b => 44 );

is the same as (note, 2nd and 3rd keys were "b" which'd overwrite the "43"
w/ "44" so I made it a "c":
( "a", 42, "b", 43, "c", 44 );

That is, the hash assignment just requires (well, Perl will assign "undef"
if you're short a value and complain if you've warnings set[1]) an even
number of elements; key, value, pairs.  You can do:
my @uvw = ( a => 42, b => 43, b => 44 );

and end up w/ 6 elements (in order) in the array.  The other thing to think
about is "context" - the LHS of the assigning "=" determines how the RHS
list is treated. In scalar context
my $one_var = (1, 2, 3);

the list returns its element count (as does an array - for hashes, it's
similar but you get a fraction: # of elements/buckets [2]) so
$one_var == 3;

In list context, you get assignment, as you see above or:
my ($one_var) = (1,2,3);

Here, the first element on the RHS gets assigned to the first member of the
LHS, i.e.
$one_var == 1;

The rest get thrown away.  If the LHS has an array, it gets everything
else, if there's too many LHS scalars, they get assigned undef.
-- 

a

[1]
# perl -we 'my %h = (b => 3, a); print "$h{a}\n";'
Unquoted string "a" may clash with future reserved word at -e line 1.
Odd number of elements in hash assignment at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1.

[2]
http://stackoverflow.com/questions/7427381/what-do-you-get-if-you-evaluate-a-hash-in-scalar-context


Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to