On 29/04/2011 10:14, Agnello George wrote:
On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon<rob.di...@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 @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 @keys = keys %{$retrn{$stdout}};
if (@keys == 1 and $stdout == $keys[0]) {
:
}
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/