Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash of hash in perl

Reply
Thread Tools

Hash of hash in perl

 
 
Shashank Khanvilkar
Guest
Posts: n/a
 
      11-17-2004
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
 
Reply With Quote
 
 
 
 
Janek Schleicher
Guest
Posts: n/a
 
      11-17-2004
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:umper;
print Dumper(\%graph);

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


Greetings,
Janek
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-17-2004
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
 
Reply With Quote
 
gumby
Guest
Posts: n/a
 
      11-18-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Perl hash and rehash seeds; deterministic hash ordering ozgune@gmail.com Perl Misc 4 01-22-2007 07:58 PM
Perl hash of hash efficiency. tak Perl Misc 28 08-07-2006 02:15 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



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