develooper Front page | perl.beginners | Postings from May 2007

Re: acccesing an hash

Thread Previous | Thread Next
May 28, 2007 17:26
Re: acccesing an hash
Message ID:
On May 28, 6:53 am, (Jeevs) wrote:
> my %hash = (jeevan=>'Ingale', Sarika =>'Bere');
> my @star = @hash{jeevan, Sarika};
> print @star;
> this prints ingale and bere but when i write

Like Paul said, the hash is being treated as an array slice returning
the values for "jeevan" and "Sarika".

> my %hash = (jeevan=>'Ingake', Sarika =>'Bere');
> my @star = %hash{jeevan, Sarika};
> print @star;

The array assignment line produces an error cos the syntax is telling
perl to unwind the entire hash (key-value pairs) but you're passing
keys to hash which under normal use will return the values. To get the
key-value pairs into @star use:

my @star = %hash;

@star will now contain the key-value pairs (Sarika, Bere, jeevan,
Ingake) but the pairs wont be in any particular order.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About