Tushar Kulkarni wrote:
> Hi,
> I have this code, but I am not getting how this works. Can someone
> help?
>
> %h = ( one => 15,
> two => 26,
> three => 37 );
>
> my @a;
Not sure what purpose this declaration serves. The 'a' referred to from here on in
the code is the hash 'a'
and not the array 'a'.
>
>
> foreach (@a {keys %h})
A hash slice is what is being used here. The above line can also be written as
foreach ($a{one}, $a{two}, $a{three}), the keys function applied on %h returns
('one', 'two', 'three').
>
> {
> $_ = $_ + 10;
The foreach loop index variable is an alias for each item in the list you are
looping over.
Changing $_ here changes the value it refers to.
perldoc perlsyn (Read through the foreach section)
>
> }
>
>
> print "New\n";
>
> foreach (@a{keys %h})
> {
> print "$_\n";
> }
>
> This prints
>
> New
> 10
> 10
> 10
>
> Can someone explain what is happening here?
>
> Thanks
> Tushar
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]