For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2007 > Accessing hash









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Accessing hash
Jeevs

2007-05-28, 6:58 pm

@hashi = @hash{qw (jeevan, sarika)};
print @hashi;

this gives me the values of keys jeevan and sarika.. how does this
work ...

Jeevs

2007-05-28, 6:58 pm

On May 28, 11:35 am, jeevan.ing...@gmail.com (Jeevs) wrote:
> @hashi = @hash{qw (jeevan, sarika)};
> print @hashi;
>
> this gives me the values of keys jeevan and sarika.. how does this
> work ...



ok i got it ...
and
I think i was not clear in my query...
I was expecting an hash slice to be
%hash{qw(jeevan sarika)} which seems more logical as i said earlier...


WEll i was browsing for more information and found out it has been
taken care of in perl6 :)

where a new operator (qoute word) is introduced <> instead of qw() in
perl5.
u can write the above hash as

@hashi = %hash<jeevan sarika>;
print @hashi;

and u can get the same output as above...


Brad Baxter

2007-05-29, 9:58 pm

On May 28, 7:41 am, jeevan.ing...@gmail.com (Jeevs) wrote:
> On May 28, 11:35 am, jeevan.ing...@gmail.com (Jeevs) wrote:
>
>

It works because that's the syntax for a hash slice--slices
use the @ to signify multiples of things.
[color=darkred]
> ok i got it ...
> and
> I think i was not clear in my query...
> I was expecting an hash slice to be
> %hash{qw(jeevan sarika)} which seems more logical as i said earlier...


Perhaps so. Which is why ...

> WEll i was browsing for more information and found out it has been
> taken care of in perl6 :)


Well, "taken care of" if you think it's broken--not everyone does.

> where a new operator (qoute word) is introduced <> instead of qw() in
> perl5.
> u can write the above hash as
>
> @hashi = %hash<jeevan sarika>;
> print @hashi;
>
> and u can get the same output as above...


For the sake of discussion:

my %hash = ( a => 1, b => 2, c => 3 );

print "%: ", %hash;
print "keys: ", keys %hash;
print "vals: ", values %hash;
print "\@1: ", @hash{ qw( a b c ) };
print "\@2: ", @hash{ keys %hash };
print "\@3: ", @hash{ sort keys %hash };

__END__
%: c3a1b2
keys: cab
vals: 312
@1: 123
@2: 312
@3: 123

Cheers,

--
Brad

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com