Home > Archive > PERL Beginners > May 2007 > acccesing an 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]
|
|
|
| my %hash = (jeevan=>'Ingale', Sarika =>'Bere');
my @star = @hash{jeevan, Sarika};
print @star;
this prints ingale and bere but when i write
my %hash = (jeevan=>'Ingake', Sarika =>'Bere');
my @star = %hash{jeevan, Sarika};
print @star;
its an error..
Can someone explain or atleast point me to any document explainng what
exactly happens heres...
| |
| Paul Lalli 2007-05-28, 6:58 pm |
| On May 28, 1:53 am, jeevan.ing...@gmail.com (Jeevs) wrote:
> my %hash = (jeevan=>'Ingale', Sarika =>'Bere');
> my @star = @hash{jeevan, Sarika};
> print @star;
>
> this prints ingale and bere but when i write
>
> my %hash = (jeevan=>'Ingake', Sarika =>'Bere');
> my @star = %hash{jeevan, Sarika};
> print @star;
>
> its an error..
>
> Can someone explain or atleast point me to any document explainng what
> exactly happens heres...
perldoc perldata
Entire arrays (and slices of arrays and hashes) are denoted
by '@', which works much like the word "these" or "those"
does in English, in that it indicates multiple values are
expected.
@days # ($days[0], $days[1],... $days[n])
@days[3,4,5] # same as ($days[3],$days[4],$days[5])
@days{'a','c'} # same as ($days{'a'},$days{'c'})
Entire hashes are denoted by '%':
%days # (key1, val1, key2, val2 ...)
Basically, you can't just make s*** up and expect it to work.
Whatever gave you the idea that '%hash{jeevan, Sarika}' was legal
syntax?
Paul Lalli
| |
| Eishbut@Googlemail.Com 2007-05-28, 9:59 pm |
| On May 28, 6:53 am, jeevan.ing...@gmail.com (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.
|
|
|
|
|