OK, Thanks. Now I have understood.
Actually I saw this code at one place and it was used to trim starting and beginning
spaces of values of hash(it
had s/\s+$// and s/^\s+// in loop instead of $_ += 10). I tried same thing with
numbers and got more confused(also
in foreach '@h' is important. I replaced it with '@a' and then I was not getting
results like original code which
added confusion). Actually I never gave a thought to slice of a hash.
Thanks again,
Tushar
Sudarsan Raghavan wrote:
> 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]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]