Hash of hash in perl

Discussion in 'Perl Misc' started by Shashank Khanvilkar, Nov 17, 2004.

  1. Hi,
    Any help is appreciated.
    I have data in the following format

    A
    B=2
    C=3
    B
    S=2
    T=10
    ....


    I need to populate a hash data structure (%graph) in perl so that %graph
    will have the form
    %graph = (

    A => {B=>2,C=>3},

    B => {S=>2,T=>10}
    }

    Assume that I have a program that has read A in $A, B in $B and so on
    and the integers in $int. Can I Do the following:

    %graph{$A}{$B} = $int

    to populate the above hash of the hash.
    Thanks
    Shashank
    Shashank Khanvilkar, Nov 17, 2004
    #1
    1. Advertising

  2. On Wed, 17 Nov 2004 16:08:07 -0600, Shashank Khanvilkar wrote:

    > I have data in the following format
    >
    > A
    > B=2
    > C=3
    > B
    > S=2
    > T=10
    > ...
    >
    >
    > I need to populate a hash data structure (%graph) in perl so that %graph
    > will have the form
    > %graph = (
    >
    > A => {B=>2,C=>3},
    >
    > B => {S=>2,T=>10}
    > }


    What have you tried so far? It's hard to spot where's your problem without
    to know something about.

    However the following snippet seems to do what you want:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my %graph;
    my $sec;

    while (<DATA>) {
    chomp;
    /^(\w+)/ and $sec = $1
    or /(\w+)=(\d+)/ and $graph{$sec}->{$1} = $2;
    }

    use Data::Dumper;
    print Dumper(\%graph);

    __DATA__
    A
    B=2
    C=3
    B
    S=2
    T=10


    Greetings,
    Janek
    Janek Schleicher, Nov 17, 2004
    #2
    1. Advertising

  3. Shashank Khanvilkar <> wrote:

    > Can I Do the following:
    >
    > %graph{$A}{$B} = $int



    What happened when you tried it?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 17, 2004
    #3
  4. Shashank Khanvilkar

    gumby Guest

    Shashank Khanvilkar <> wrote in message news:<cngjth$7h1$>...
    > Hi,
    > Any help is appreciated.
    > I have data in the following format
    >
    > A
    > B=2
    > C=3
    > B
    > S=2
    > T=10
    > ...
    >
    >
    > I need to populate a hash data structure (%graph) in perl so that %graph
    > will have the form
    > %graph = (
    >
    > A => {B=>2,C=>3},
    >
    > B => {S=>2,T=>10}
    > }
    >
    > Assume that I have a program that has read A in $A, B in $B and so on
    > and the integers in $int. Can I Do the following:
    >
    > %graph{$A}{$B} = $int
    >
    > to populate the above hash of the hash.
    > Thanks
    > Shashank


    If you could give an example snipit of the code you are writing to
    show how you are using the hash of hash.
    I use these alot.

    The question will this work ?
    %graph{$A}{$B} = $int
    Yes but you should do it like this
    $graph{$A}{$B} = $int;
    Extracting the keys might be the only speed bump.
    my @akeys = keys %graph;
    my @bkeys = keys % {$graph{$A}};

    Hope this helps
    gumby, Nov 18, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. rp
    Replies:
    1
    Views:
    494
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    596
    David A. Black
    Jul 2, 2008
  4. tak

    Perl hash of hash efficiency.

    tak, Aug 2, 2006, in forum: Perl Misc
    Replies:
    28
    Views:
    429
    Ian Wilson
    Aug 7, 2006
  5. Replies:
    4
    Views:
    258
Loading...

Share This Page