Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding total hashes within the array in a hash of arrays of hashes 1

Status
Not open for further replies.

sloppyhack

Technical User
Apr 17, 2001
111
US
I built a hash of arrays of hashes. I can access everything, but I need to do something with each hash within the array of hashes and am having trouble defining how many hashes exist in the array so I can use a 'for' loop to get each one.

The structure looks like.....

$hash{$hashname}[$row]->{'VALUE'} = 'SOMETHING';

I'm trying to get the total elements in the array with the following but it's not working.

$totalinarray = scalar $hash{$hashname};

Anyone know how to get to the total number of hashes in the array? Or..a good way to cycle through the hashes?

Cheers,

Sloppyhack
 
Try something like this:

Code:
my %hash;
$hash{simpsons}[0]->{Homer} = "Dad";
$hash{simpsons}[1]->{Marge} = "Mom";
$hash{simpsons}[2]->{Bart} = "Son";
$hash{simpsons}[3]->{Lisa} = "Daughter";
$hash{simpsons}[4]->{Maggie} = "Baby";

my $array_elements = @{$hash{simpsons}};
print $array_elements, "\n";
 
I'm just curious, what type of data do you have that uses such a data set as you posted:

$hash{$hashname}[$row]->{'VALUE'} = 'SOMETHING';

It seems awkward to me but maybe it isn't.
 
Are you sure that these hashes of yours are not references?
Did it came from parsing some kind of xml?
Who on earth would need a structure like this.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Code:
use strict;
use warnings;
use Data::Dumper;

print Dumper(%hash);
will show you what the structure looks like. If you post a bit of the output, maybe we can advise?

Also, rather than using a for loop with a counter, why not just use a foreach? Then you don't have to get the count, or use a loop counter variable.
 
rharsh's suggestion did it.

$total = scalar @{$hash{$name}};

Thanks rharsh!!!! And thanks to everyone who chimed in.

As for why I'm using such an eleborate data structure perluserpengo.....I have an application that determines differences in field values between a flat file and data in a database. The goal is to get results to help sychronize the database info. I have to first read the incoming file and analyze the data within (array of hashes)...I then need to group those records based on normalizing some key information (pattern matching algos)...and group the items based on that value (hash of array of hashes). This grouping of records is necessary for efficiency and memory usage. The database I'm comparing against has millions of records and I can't read it all into memory at once..have to do it by groupings. Example...a file with 60K products from 100s of vendors...read file and group the items by normalizing the vendor name...then compare product info to product info from the same vendor within the database. I was using an array reference to do this but 'use strict;' doesn't like array references..so I need to put the 'array of hashes' into a hash based on the normalized vendor name. Sounds crazy but it's really pretty slick..and fast. The 3-D structure allows me to do just about anything I want to with the data.

Thanks again everyone.

Cheers,

Sloppyhack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top