retrieving key from hash of hashes
Hi All
I got a hash like this :
my %retrn = ( 0 => { 0 => ' successful<br>'},
1 => { 1 => 'insufficient<br>'},
2 => { 2 => 'txtfile missing<br>'},
3 => { 3 => 'bad dir <br>'},
);
( i know this hash looks funny , but is the hash i got to use )
suppose $stdout = 0;
i need to get the key
my key = keys %{ $retrn{ $stdout} } ;
but i am not getting the desired out put which should be '0'
what am i doing wrong .
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
Agnello George wrote:
> Hi All
Hello,
> I got a hash like this :
>
>
> my %retrn = ( 0 => { 0 => ' successful<br>'},
> 1 => { 1 => 'insufficient<br>'},
> 2 => { 2 => 'txtfile missing<br>'},
> 3 => { 3 => 'bad dir<br>'},
> );
>
> ( i know this hash looks funny , but is the hash i got to use )
>
> suppose $stdout = 0;
>
>
> i need to get the key
>
> my key = keys %{ $retrn{ $stdout} } ;
That should probably be:
my $key = keys %{ $retrn{ $stdout} } ;
> but i am not getting the desired out put which should be '0'
>
> what am i doing wrong .
You are forcing scalar context with your assignment and keys() in scalar
context returns the number of keys in the hash, which in this case is 1
because you only have one key.
You need to force list context on the assignment which will return the
actual keys from the hash:
my ( $key ) = keys %{ $retrn{ $stdout} } ;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
On 29/04/2011 09:47, Agnello George wrote:
>
> my %retrn = ( 0 => { 0 => ' successful<br>'},
> 1 => { 1 => 'insufficient<br>'},
> 2 => { 2 => 'txtfile missing<br>'},
> 3 => { 3 => 'bad dir<br>'},
> );
>
> ( i know this hash looks funny , but is the hash i got to use )
>
> suppose $stdout = 0;
>
>
> i need to get the key
>
> my key = keys %{ $retrn{ $stdout} } ;
I assume this should read
my $key = keys %{ $retrn{ $stdout} } ;
In list context the keys operator returns the list of all keys of a
hash. But in a scalar context, as here, it returns the number of keys
instead. I assume you are getting a value of 1 in $key when you are
expecting 0?
If there is always only a single key then you can write instead
my ($key) = keys %{ $retrn{ $stdout} } ;
which will extract the first key of the list returned by keys. If you
can't guarantee that then you should assign to an array instead:
my [at] keys = keys %{ $retrn{ $stdout} } ;
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon <rob.dixon [at] gmx.com> wrote:
> On 29/04/2011 09:47, Agnello George wrote:
>>
>> =A0my %retrn =3D ( 0 =3D> =A0{ 0 =3D> =A0' =A0successful<br>'},
>> =A0 =A0 =A0 =A0 1 =3D> =A0{ 1 =3D> =A0'insufficient<br>'},
>> =A0 =A0 =A0 =A0 2 =3D> =A0{ 2 =3D> =A0'txtfile missing<br>'},
>> =A0 =A0 =A0 =A0 3 =3D> =A0{ 3 =3D> =A0'bad dir<br>'},
>> =A0 =A0 =A0 =A0 );
>>
>> ( i know this hash looks funny , but is the hash i got to use )
>>
>> suppose $stdout =3D 0;
>>
>>
>> i need to get the key
>>
>> my key =3D keys %{ $retrn{ $stdout} } ;
>
> I assume this should read
>
> =A0my $key =3D keys %{ $retrn{ $stdout} } ;
>
> In list context the keys operator returns the list of all keys of a
> hash. But in a scalar context, as here, it returns the number of keys
> instead. I assume you are getting a value of 1 in $key when you are
> expecting 0?
>
> If there is always only a single key then you can write instead
>
> =A0my ($key) =3D keys %{ $retrn{ $stdout} } ;
>
> which will extract the first key of the list returned by keys. If you
> can't guarantee that then you should assign to an array instead:
>
> =A0my [at] keys =3D keys %{ $retrn{ $stdout} } ;
>
Ok , i see my fault , so i can also do something like this right
if (stdout =3D=3D (%$retrn{$stdout}) ) {
##so some code
}
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
On Fri, Apr 29, 2011 at 2:44 PM, Agnello George
<agnello.dsouza [at] gmail.com> wrote:
> On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon <rob.dixon [at] gmx.com> wrote:
>> On 29/04/2011 09:47, Agnello George wrote:
>>>
>>> =A0my %retrn =3D ( 0 =3D> =A0{ 0 =3D> =A0' =A0successful<br>'},
>>> =A0 =A0 =A0 =A0 1 =3D> =A0{ 1 =3D> =A0'insufficient<br>'},
>>> =A0 =A0 =A0 =A0 2 =3D> =A0{ 2 =3D> =A0'txtfile missing<br>'},
>>> =A0 =A0 =A0 =A0 3 =3D> =A0{ 3 =3D> =A0'bad dir<br>'},
>>> =A0 =A0 =A0 =A0 );
>>>
>>> ( i know this hash looks funny , but is the hash i got to use )
>>>
>>> suppose $stdout =3D 0;
>>>
>>>
>>> i need to get the key
>>>
>>> my key =3D keys %{ $retrn{ $stdout} } ;
>>
>> I assume this should read
>>
>> =A0my $key =3D keys %{ $retrn{ $stdout} } ;
>>
>> In list context the keys operator returns the list of all keys of a
>> hash. But in a scalar context, as here, it returns the number of keys
>> instead. I assume you are getting a value of 1 in $key when you are
>> expecting 0?
>>
>> If there is always only a single key then you can write instead
>>
>> =A0my ($key) =3D keys %{ $retrn{ $stdout} } ;
>>
>> which will extract the first key of the list returned by keys. If you
>> can't guarantee that then you should assign to an array instead:
>>
>> =A0my [at] keys =3D keys %{ $retrn{ $stdout} } ;
>>
>
> Ok , i see my fault , so i can also do something like this right
>
> =A0if (stdout =3D=3D =A0 (%$retrn{$stdout}) =A0) {
> =A0 =A0 =A0 ##so some code
> =A0 =A0}
>
sorry i ment
if (stdout =3D=3D keys (%$retrn{$stdout}) ) {
##so some code
}
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
On 29/04/2011 10:14, Agnello George wrote:
> On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon<rob.dixon [at] gmx.com> wrote:
>> On 29/04/2011 09:47, Agnello George wrote:
>>>
>>> my %retrn = ( 0 => { 0 => ' successful<br>'},
>>> 1 => { 1 => 'insufficient<br>'},
>>> 2 => { 2 => 'txtfile missing<br>'},
>>> 3 => { 3 => 'bad dir<br>'},
>>> );
>>>
>>> ( i know this hash looks funny , but is the hash i got to use )
>>>
>>> suppose $stdout = 0;
>>>
>>>
>>> i need to get the key
>>>
>>> my key = keys %{ $retrn{ $stdout} } ;
>>
>> I assume this should read
>>
>> my $key = keys %{ $retrn{ $stdout} } ;
>>
>> In list context the keys operator returns the list of all keys of a
>> hash. But in a scalar context, as here, it returns the number of keys
>> instead. I assume you are getting a value of 1 in $key when you are
>> expecting 0?
>>
>> If there is always only a single key then you can write instead
>>
>> my ($key) = keys %{ $retrn{ $stdout} } ;
>>
>> which will extract the first key of the list returned by keys. If you
>> can't guarantee that then you should assign to an array instead:
>>
>> my [at] keys = keys %{ $retrn{ $stdout} } ;
>>
>
> Ok , i see my fault , so i can also do something like this right
>
> if (stdout == (%$retrn{$stdout}) ) {
> ##so some code
> }
Again, I think you mean $stdout? And you would have to write
%{$retrn{$stdout}} (as you hade before otherwise you would be referring
to a scalar $retrn that doesn't exist.
After that, the code doesn't make much sense I'm afraid, as you are
comparing a scalar with a hash, which doesn't do much useful at all.
You could write
if ($stdout == (keys %{$retrn{$stdout}})[0]) {
:
}
As long as you were certain there would only be one key/value pair in
the hash, but I would rather see
my [at] keys = keys %{$retrn{$stdout}};
if ( [at] keys == 1 and $stdout == $keys[0]) {
:
}
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
>>>>> "AG" == Agnello George <agnello.dsouza [at] gmail.com> writes:
AG> sorry i ment
AG> if (stdout == keys (%$retrn{$stdout}) ) {
AG> ##so some code
AG> }
i don't think you are getting it yet. keys gets you the list of keys
ONLY in a list context. == provides a scalar context which will get you
the number of keys in the hash. i can't see your needing the number of
keys and then comparing that to stdout. also why is stdout a bareword?
it isn't the handle STDOUT so it won't work under strict. why don't you
tell us what your real goal is and not your solution?
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: retrieving key from hash of hashes
On 29/04/2011 10:15, Agnello George wrote:
>
> sorry i ment
>
> if (stdout == keys (%$retrn{$stdout}) ) {
> ##so some code
> }
Erm, perhaps
if ($stdout == keys %{$retrn{$stdout}}) {
But that would compare the value of $stdout with the /number/ of keys
in the hash, which is always one in your original example.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/