in reply to How to Hash Hashes

You probably don't actually want a hash of hash (HoH), but a HoA. Consider:

use strict; use warnings; use Data::Dumper; my $inst; my %host; while(<DATA>) { if (/.*\/DISK\.(.*)\/.*/) { $inst = $1; next; } my ($dayofweek, $month, $date, $timing, $year, $dpoint) = split; push @{$host{$inst}}, { 'dayofweek' => $dayofweek, 'month' => $month, 'date' => $date, 'year' => $year, 'timing' => $timing, 'dpoint' => $dpoint }; } print Dumper(\%host); __DATA__ wspmon03/DISK.ssd123/DSKAvgServ Wed Nov 30 00:01:27 2006 7.34562 Wed Nov 30 00:02:29 2006 3.31222 Wed Nov 30 00:03:33 2006 1.23511 Wed Nov 30 00:04:23 2006 4.97532 Wed Nov 30 00:05:37 2006 5.31112 Wed Nov 30 00:06:36 2006 6.22991 wspmon03/DISK.ssd126/DSKAvgServ Wed Nov 30 00:01:27 2006 7.34562 Wed Nov 30 00:02:29 2006 3.31222 Wed Nov 30 00:03:33 2006 1.23511 Wed Nov 30 00:04:23 2006 4.97532 Wed Nov 30 00:05:37 2006 5.31112 Wed Nov 30 00:06:36 2006 6.22991 wspmon03/DISK.ssd129/DSKAvgServ Wed Nov 30 00:01:27 2006 7.34562 Wed Nov 30 00:02:29 2006 3.31222 Wed Nov 30 00:03:33 2006 1.23511 Wed Nov 30 00:04:23 2006 4.97532 Wed Nov 30 00:05:37 2006 5.31112 Wed Nov 30 00:06:36 2006 6.22991

Prints:

$VAR1 = { 'ssd129' => [ { 'timing' => '00:01:27', 'dpoint' => '7.34562', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:02:29', 'dpoint' => '3.31222', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:03:33', 'dpoint' => '1.23511', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:04:23', 'dpoint' => '4.97532', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:05:37', 'dpoint' => '5.31112', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:06:36', 'dpoint' => '6.22991', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' } ], 'ssd126' => [ { 'timing' => '00:01:27', 'dpoint' => '7.34562', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:02:29', 'dpoint' => '3.31222', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:03:33', 'dpoint' => '1.23511', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:04:23', 'dpoint' => '4.97532', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:05:37', 'dpoint' => '5.31112', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:06:36', 'dpoint' => '6.22991', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' } ], 'ssd123' => [ { 'timing' => '00:01:27', 'dpoint' => '7.34562', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:02:29', 'dpoint' => '3.31222', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:03:33', 'dpoint' => '1.23511', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:04:23', 'dpoint' => '4.97532', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:05:37', 'dpoint' => '5.31112', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' }, { 'timing' => '00:06:36', 'dpoint' => '6.22991', 'date' => '30', 'month' => 'Nov', 'dayofweek' => 'Wed', 'year' => '2006' } ] };

which has the advantage that you don't need to maintain a bogus item count to 'index' into your 'hash' for the individual server information.

As an aside, your sample code had a nasty bug in it. You redefine $inst inside an if block, assigned a value to it (from $1), then throw the value away when the local instance of $inst goes out of scope at the end of the block.

Update: readmore tags added by popular demand.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: How to Hash Hashes
by onegative (Scribe) on Nov 30, 2006 at 15:37 UTC
    Yes, Yes, Yes...Grandfather
    It took me a little while to comprehend what you coded but it makes sense to me now...seeing I can access everything independently based off Instance, Array Count and data_id...sweet...this is exactly what I was wanting to accomplish...I will definately put this knowledge to good use.
    $host{sd1}[0]{dpoint} $host{sd1}[1]{dpoint} $host{sd1}[2]{dpoint} $host{sd1}[3]{dpoint} $host{sd1}[4]{dpoint} $host{sd1}[5]{dpoint} $host{sd2}[0]{dpoint} $host{sd2}[1]{dpoint} $host{sd2}[2]{dpoint} $host{sd2}[3]{dpoint} $host{sd2}[4]{dpoint} $host{sd2}[5]{dpoint} $host{sd3}[0]{dpoint} $host{sd3}[1]{dpoint} $host{sd3}[2]{dpoint} $host{sd3}[3]{dpoint} $host{sd3}[4]{dpoint} $host{sd3}[5]{dpoint}
    Thank you so much Grandfather... And as a side note, your Grandson is a handsome fellow...I am sure you are very proud of him.
    I appreciate your help tremendously and take care.