FAQ
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

Search Discussions

  • John W. Krahn at Apr 29, 2011 at 8:59 am

    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
  • Rob Dixon at Apr 29, 2011 at 9:03 am

    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} } ;

    HTH,

    Rob
  • Agnello George at Apr 29, 2011 at 9:14 am

    On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon 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
    }



    --
    Regards
    Agnello D'souza
  • Agnello George at Apr 29, 2011 at 9:15 am

    On Fri, Apr 29, 2011 at 2:44 PM, Agnello George wrote:
    On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixon 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
    }
    sorry i ment

    if (stdout == keys (%$retrn{$stdout}) ) {
    ##so some code
    }






    --
    Regards
    Agnello D'souza
  • Uri Guttman at Apr 29, 2011 at 9:25 am
    "AG" == Agnello George 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@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Rob Dixon at Apr 29, 2011 at 9:26 am

    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
  • Rob Dixon at Apr 29, 2011 at 9:24 am

    On 29/04/2011 10:14, Agnello George wrote:
    On Fri, Apr 29, 2011 at 2:33 PM, Rob Dixonwrote:
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 29, '11 at 8:47a
activeApr 29, '11 at 9:26a
posts8
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase