On Wed, Oct 13, 2010 at 12:34:19PM -0700, C.DeRykus wrote:
> On Oct 13, 9:40?am, [email protected] (Mike McClain) wrote:
<snip>
> > > On Wednesday 13 October 2010 06:39:03 Mike McClain wrote:
> > > > Why do @arrays and @seconds not have the same number of elements?
> > > > my @arrays =
> > > > map
> > > > { @{ $HoAoA{$_} } [ 0..$#{ $HoAoA{$_} } ] }
> > > > keys %HoAoA ;
> >
> > > > my @seconds =
> > > > map { @{ $HoAoA{$_} } [ 0..$#{ $HoAoA{$_} } ]->[1] }
> > > > keys %HoAoA ;
> > What I still don't understand is why it gives me what it does.
>
> It's easier to understand what went wrong by reviewing
> the arrow operator (see: perlref)
> ...
> $arrayref->[0] = "January"; # Array element
> $hashref->{"KEY"} = "VALUE"; # Hash element
> $coderef->(1,2,3); # Subroutine call
>
> The left side of the arrow can be any expression
> returning a reference, including a previous deref...
> ^^^^^^^^^^
Since @arrays is an array of references it didn't dawn on me that
the '->[1]' would put me in scalar context.
> and the comma operator (see: perlop)
>
> Binary "," is the comma operator. In scalar context it
> evaluates its left argument, throws that value away,
> then evaluates its right argument and returns that value.
> ...
This threw me for a loop for a while since there is no comma in
map { @{ $HoAoA{$_} } [ 0..$#{ $HoAoA{$_} } ]->[1] } keys %HoAoA;
but I figured out that the range operator '..' returns a list and
eventually found the defination of list in perldata.
I was surprised it didn't show up in perlintro nor in the index of
'Programming Perl' and 'Learning Perl' calls it a 'List Literal' in
chapter 3 which leads to confusion.
>
> In your case, the left side is an array slice which
> generates a list of values. Because the context is
> scalar, the comma operator reduces the expanded
> slice list down to [ qw/bc1 bc2/] for the 2nd key for
> instance. That just happens to be a reference so
> the arrow operator is happy. Ditto for the first key
> too:
>
> 1st key: [qw/ab1 ab2/]->[1] ---> ab2
> 2nd key: [qw/bc1 bc2/ ]->[1] ---> bc2
>
>
>
> A simpler pair of examples might help:
>
> # ok because the comma op reduces the slice to a ref
> perl -Mstrict -wle "my @a=([1,2],[3,4]);print @a[0..$#a]->[1]"
> 4
This simple example showed me the answer to my question.
Thank you, Sir.
Mike McClain
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/