Professional Web Applications Themes

How to access first key of "Hash of Hash" - PERL Beginners

Hi, I have this HoH: my %HoH = ( firstkey => { A => 'blabla', B => 'dadada', C => 'tititi',} ); generated with $HoH{$fkey}{$alpha}=$text; how can I access the value of the first key, so that it gives: "firstkey" I tried this with no avail: print "$HoH{$fkey}\n" Regards, Edward WIJAYA SINGAPORE...

  1. #1

    Default How to access first key of "Hash of Hash"

    Hi,

    I have this HoH:
    my %HoH = (
    firstkey => { A => 'blabla',
    B => 'dadada',
    C => 'tititi',}
    );

    generated with

    $HoH{$fkey}{$alpha}=$text;

    how can I access the value
    of the first key, so that it gives: "firstkey"

    I tried this with no avail:

    print "$HoH{$fkey}\n"



    Regards,
    Edward WIJAYA
    SINGAPORE
    Edward Guest

  2. #2

    Default Re: How to access first key of "Hash of Hash"

    > Hi, 

    This is confusing, the value of the first key is a hash, so it will not
    give 'firstkey'.
     

    RIght, that will print info about the HASH (aka the reference
    stringified). Looks like you are walking into the world of references,
    have you read:

    perldoc perllol
    perldoc perldsc
    perldoc perlreftut
    perldoc perlref

    They should get you going. Of course it might be sufficient to start with,

    perldoc -f keys
    perldoc -f values
    perldoc -f each

    But if not, try to provide more specifics of what you are up to.
     

    http://danconia.org
    Wiggins Guest

  3. #3

    Default Re: How to access first key of "Hash of Hash"

    On 29 Sep 2004 14:58:00 +0100, Jose Alves de Castro
    <di.uminho.pt> wrote: 

    So sorry for being not clear.
    I will extend just a bit.

    Suppose I have:

    my %HoH = (
    firstkey => { A => 'blabla',
    B => 'dadada',
    C => 'tititi',}
    secondkey => { D => 'blabla',
    E => 'dadada',
    F => 'tititi',}

    );

    and I generated that HoH with this:

    $HoH{$fkey}{$alpha}=$text;

    namely:

    "firstkey, secondkey" from $fkey
    "A, B, C, etc" from $alpha
    "blabla etc" from $text

    my question is how can I print output like:

    firstkey
    secondkey

    given the construction variables as mention before.

    Thanks

    Regards,
    Edward WIJAYA
    SINGAPORE
    Edward Guest

  4. #4

    Default Re: How to access first key of "Hash of Hash"

    On 29 Sep 2004 15:20:39 +0100, Jose Alves de Castro
    <di.uminho.pt> wrote:

     

    It seems so. Thanks a lot.
    I thought 'keys' are only for simple hash.

    Regards,
    Edward WIJAYA
    SINGAPORE
    Edward Guest

  5. #5

    Default Re: How to access first key of "Hash of Hash"

    Edward Wijaya wrote: 
    >
    > So sorry for being not clear.
    > I will extend just a bit.
    >
    > Suppose I have:
    >
    > my %HoH = (
    > firstkey => { A => 'blabla',
    > B => 'dadada',
    > C => 'tititi',}
    > secondkey => { D => 'blabla',
    > E => 'dadada',
    > F => 'tititi',}
    >
    > );
    >
    > and I generated that HoH with this:
    >
    > $HoH{$fkey}{$alpha}=$text;
    >
    > namely:
    >
    > "firstkey, secondkey" from $fkey
    > "A, B, C, etc" from $alpha
    > "blabla etc" from $text
    >
    > my question is how can I print output like:
    >
    > firstkey
    > secondkey[/ref]

    How about:

    print "$_\n" foreach( keys( %HoH ) );

    or

    map{ print "$_\n" } keys( %HoH );

    Remember that a hash stores its keys/values in random order, so you will get
    your keys in random order.
     


    --
    Flemming Greve Skovengaard The prophecy of the holy Norns
    a.k.a Greven, TuxPower A tale of death and doom
    <cybercity.dk> Odin saw the final sign
    4112.38 BogoMIPS The end is coming soon
    Flemming Guest

  6. #6

    Default Re: How to access first key of "Hash of Hash"

    On Wed, 29 Sep 2004 22:18:49 +0800, Edward Wijaya
    <com.sg> wrote: 
    >
    > So sorry for being not clear.
    > I will extend just a bit.
    >
    > Suppose I have:
    >
    > my %HoH = (
    > firstkey => { A => 'blabla',
    > B => 'dadada',
    > C => 'tititi',}
    > secondkey => { D => 'blabla',
    > E => 'dadada',
    > F => 'tititi',}
    >
    > );
    >
    > and I generated that HoH with this:
    >
    > $HoH{$fkey}{$alpha}=$text;
    >
    > namely:
    >
    > "firstkey, secondkey" from $fkey
    > "A, B, C, etc" from $alpha
    > "blabla etc" from $text
    >
    > my question is how can I print output like:
    >
    > firstkey
    > secondkey
    >
    > given the construction variables as mention before.
    >
    > Thanks
    >
    > Regards,
    > Edward WIJAYA
    > SINGAPORE
    >
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]

    Jose was correct. You need to read:

    perldoc -f keys

    try this:

    print "$_\n" foreach( keys %HoH );

    --Errin
    Errin Guest

  7. #7

    Default RE: How to access first key of "Hash of Hash"

    Edward Wijaya wrote: 

    All hashes are simple hashes, if you think about it. Each entry is a string
    key and a scalar value. There is no other kind of hash.

    Now that scalar value can be a *reference* to something else, and you get a
    bit of syntactic sugar for dereferencing it. But it's still just a scalar.
    Bob Guest

Similar Threads

  1. Replies: 3
    Last Post: July 8th, 07:44 AM
  2. Replies: 24
    Last Post: January 28th, 03:10 PM
  3. Sort a hash based on values in the hash stored as arrays of hashes
    By Tore Aursand in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 16th, 10:14 AM
  4. Detecting duplicate keys in a hash I am "requiring"
    By Gupit in forum PERL Miscellaneous
    Replies: 9
    Last Post: September 11th, 11:17 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139