Click here to get back home

Help with Hash of Hashes

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with Hash of Hashes Kevin Brammer 06-17-2008
Posted by Kevin Brammer on June 17, 2008, 12:25 am
Please log in for more thread options

I'm trying to write what I thought would be a simple script for work. I
can visualize the data structure I need in my head, but coding it is
another story. Basically, I'm parsing a text file which is the output
of a web query. The format is basically:

00-00AAAA
- string,0,0,0,0,0
- string,0,0,0,0,0

The thing is, each "item" (the 00-00AAAA) can have 0-4 entries below it,
semi-csv formatted, default value of "0".

My data structure in my head was a hash of hashes:

%HoH = ( item => {
                entry1 => "0",
                entry2 => "0",
                entry3 => "0",
                }
        );

Problem is, I would like it to be dynamic enough to pull the "item" from
the file and not have to "declare" the name ahead of time. I'm using
strict, and am careful about data input and validation. So, in this
case I would have:

%HoH = ( "00-00AAAA" => {
                entry1 => "string,0,0,0,0,0",
                entry2 => "string,0,0,0,0,0",
                entry3 => "0",
                ....
                }
        );

I was able to do it using a %HoH = (); and then just building the hash
as I went along, but is that "correct"?





Posted by nolo contendere on June 17, 2008, 9:48 am
Please log in for more thread options
wrote:
> I'm trying to write what I thought would be a simple script for work. =A0I=

> =A0 can visualize the data structure I need in my head, but coding it is
> another story. =A0Basically, I'm parsing a text file which is the output
> of a web query. =A0The format is basically:
>
> 00-00AAAA
> =A0 - string,0,0,0,0,0
> =A0 - string,0,0,0,0,0
>
> The thing is, each "item" (the 00-00AAAA) can have 0-4 entries below it,
> semi-csv formatted, default value of "0".
>
> My data structure in my head was a hash of hashes:
>
> %HoH =3D ( =A0item =3D> {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry1 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry2 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry3 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 );
>
> Problem is, I would like it to be dynamic enough to pull the "item" from
> the file and not have to "declare" the name ahead of time. =A0I'm using
> strict, and am careful about data input and validation. =A0So, in this
> case I would have:
>
> %HoH =3D ( "00-00AAAA" =3D> {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry1 =3D> "string,0,0,0,0,0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry2 =3D> "string,0,0,0,0,0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry3 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ....
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 );
>
> I was able to do it using a %HoH =3D (); and then just building the hash
> as I went along, but is that "correct"?

Here's a quick stab at a solution--not elegant by any means...also, i
added an "entry0" for every key, so even if the data doesn't have an
entry, the "item" will still show up in the hash.

#!/usr/bin/perl

use strict; use warnings;
use Data::Dumper;

my %h;
my $last_key;
my $entry_no;
while ( <DATA> ) {
chomp;
if ( /^(\S+)/ ) {
$last_key =3D $1;
$entry_no =3D 0;
$h =3D 0;
}
else {
my ( $val ) =3D /.+?\-\s(.*)$/;
if ( $last_key ) {
$entry_no++;
$h =3D $val;
}
}

}

print Dumper( \%h ), "\n";

__DATA__
00-00AAAA
- string,0,0,0,0,0
- string,0,0,0,0,0
1-A
- blah,0
2-B
3-C
- C,1
- C,2
- C,3



> ./hoh.pl
$VAR1 =3D {
'1-A' =3D> {
'entry0' =3D> 0,
'entry1' =3D> 'blah,0'
},
'2-B' =3D> {
'entry0' =3D> 0
},
'00-00AAAA' =3D> {
'entry2' =3D> 'string,0,0,0,0,0',
'entry0' =3D> 0,
'entry1' =3D> 'string,0,0,0,0,0'
},
'3-C' =3D> {
'entry2' =3D> 'C,2',
'entry0' =3D> 0,
'entry3' =3D> 'C,3',
'entry1' =3D> 'C,1'
}
};


Posted by bugbear on June 17, 2008, 9:56 am
Please log in for more thread options
Kevin Brammer wrote:
>
> I'm trying to write what I thought would be a simple script for work. I
> can visualize the data structure I need in my head, but coding it is
> another story. Basically, I'm parsing a text file which is the output
> of a web query. The format is basically:
>
> 00-00AAAA
> - string,0,0,0,0,0
> - string,0,0,0,0,0
>
> The thing is, each "item" (the 00-00AAAA) can have 0-4 entries below it,
> semi-csv formatted, default value of "0".
>
> My data structure in my head was a hash of hashes:
>
> %HoH = ( item => {
> entry1 => "0",
> entry2 => "0",
> entry3 => "0",
> }
> );

Surely you want a hash of arrays?

%HoA = { item => [
"0",
"0",
"0",
]
};

BugBear

Posted by cartercc on June 17, 2008, 10:18 am
Please log in for more thread options
wrote:
> I'm trying to write what I thought would be a simple script for work. I
> can visualize the data structure I need in my head, but coding it is
> another story. Basically, I'm parsing a text file which is the output
> of a web query. The format is basically:
>
> 00-00AAAA
> - string,0,0,0,0,0
> - string,0,0,0,0,0

I solved an identical probelm with a hash array references. My file
contained open seats in course sections, like this:
BIO-1101 ZA1:20 ZA2:20 ZA3:20
ENG-1101 ZA1:25 ZA2:25

To write, I created a hash of courses (like BIO-1101) with a value
comprised of a reference to an array, and then pushed each section to
the array for each hash.

To read, I iterated through the hash created the courses, then split
the array elements on the colon, then iterated through that, like
this:

my ($crs, $sec)
foreach $crs (sort keys %open_seats)
foreach $sec (sort keys %})
print "$crs $sec $open_seats\n"

The data structure looks like this:
BIO-1101 => [ZA1:20, ZA2:20, ZA3:30]

CC

Posted by nolo contendere on June 17, 2008, 10:54 am
Please log in for more thread options
wrote:
> I'm trying to write what I thought would be a simple script for work. =A0I=

> =A0 can visualize the data structure I need in my head, but coding it is
> another story. =A0Basically, I'm parsing a text file which is the output
> of a web query. =A0The format is basically:
>
> 00-00AAAA
> =A0 - string,0,0,0,0,0
> =A0 - string,0,0,0,0,0
>
> The thing is, each "item" (the 00-00AAAA) can have 0-4 entries below it,
> semi-csv formatted, default value of "0".
>
> My data structure in my head was a hash of hashes:
>
> %HoH =3D ( =A0item =3D> {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry1 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry2 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry3 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 );
>
> Problem is, I would like it to be dynamic enough to pull the "item" from
> the file and not have to "declare" the name ahead of time. =A0I'm using
> strict, and am careful about data input and validation. =A0So, in this
> case I would have:
>
> %HoH =3D ( "00-00AAAA" =3D> {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry1 =3D> "string,0,0,0,0,0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry2 =3D> "string,0,0,0,0,0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 entry3 =3D> "0",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ....
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 );
>
> I was able to do it using a %HoH =3D (); and then just building the hash
> as I went along, but is that "correct"?

For the record, I agree with bugbear that a more appropriate data
structure would be a hash of arrays.

Similar ThreadsPosted
Performance Improvement of complex data structure (hash of hashes of hashes) August 24, 2004, 2:26 pm
Re: Performance Improvement of complex data structure (hash of hashes of hashes) August 25, 2004, 10:49 am
Re: Performance Improvement of complex data structure (hash of hashes of hashes) August 26, 2004, 7:19 am
Re: Performance Improvement of complex data structure (hash of hashes of hashes) August 26, 2004, 7:26 am
"Pseudo-hashes are deprecated" error and accessing a hash of hashes January 30, 2006, 4:04 pm
Hash of hashes, of hashes, of arrays of hashes October 27, 2005, 6:05 pm
Hashes of hashes or just one hash ? June 8, 2005, 6:09 am
Sorting a hash containing a hash of hashes December 14, 2005, 2:29 pm
Help with Hash of Hashes March 1, 2006, 1:54 pm
Hash of hashes April 10, 2006, 5:15 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap