Although I use perl fairly regularly, I don't have to create complex hash tables very often. In addition, I can usually heavily leverage hash table examples I have in pre-existing scripts. Every so often, though, I have to create a hash table that doesn't look much like anything I have, and I get stuck pretty easily. Alas, it has happened again. I am creating a table that is formatted like so:-
As you can see, the key is a simple string. But I need to split the key to properly work with its contents. Here is what I am doing:-$NetNameAndFanout = join (" ",$NetName,$NetFanout); $NetStatsHash{$NetNameAndFanout} = join (" ",$NetCap,$NetRes,$NetLengt +h,$FirstDriver);
...but I just don't like it. I really want to be able to sort the table by $NetFanout, an integer, without having to split the master key every time. In addition, when I get to $KeyFanout = $UpperFanoutLimitOfTable + 1, I want to quickly exit out of the master for ($i) loop. (I think that will be easy to manage once the hash is sorted properly.)for ($i = 1; $i <= $UpperFanoutLimitOfTable; $i++) { $TotalCap[$i] = 0; $TotalRes[$i] = 0; $TotalLength[$i] = 0; $FanoutCount = 0; foreach $MasterKey (keys %NetStatsHash) { @KeyEntries = split (/\s+/,$MasterKey); $KeyNetName = $KeyEntries[0]; $KeyFanout = $KeyEntries[1]; $OkayToAdd = 0; if ($KeyFanout == $i) { @NetDetails = split (/\s+/,$NetStatsHash{$MasterKey}); $NetCap = $NetDetails[0]; $NetRes = $NetDetails[1]; $NetLength = $NetDetails[2]; $FirstDriver = $NetDetails[3]; if ($DebugMode == 1) {print ("$i $NetCap $NetRes $NetLengt +h $FirstDriver\n");} if (($UseDriverCell == 0) && ($UseNetPattern == 0)) {$Okay +ToAdd = 1;} if ($UseDriverCell == 1) { if ($FirstDriver =~ /$DriverPattern/) { $OkayToAdd = 1; } } if ($UseNetPattern == 1) { if ($KeyNetName =~ /$NetPattern/) { $OkayToAdd = 1; } } if ($OkayToAdd == 1) { if ($DebugMode == 1) {print ("Processing net $KeyNetNa +me (driver = $FirstDriver)...\n");} $TotalCap[$i] = $TotalCap[$i] + $NetCap; $TotalRes[$i] = $TotalRes[$i] + $NetRes; $TotalLength[$i] = $TotalLength[$i] + $NetLength; $FanoutCount++; } } } if ($FanoutCount > 0) { $CapAvg[$i] = $TotalCap[$i]/$FanoutCount; $ResAvg[$i] = $TotalRes[$i]/$FanoutCount; $LengthAvg[$i] = $TotalLength[$i]/$FanoutCount; } if ($FanoutCount == 0) { print ("No nets match input criteria when fanout = $i. Please + review command-line settings--they may be too restrictive!\n"); } if ($DebugMode == 1) { printf ("CapAvg[$i] = %0.6f\n",$CapAvg[$i]); printf ("ResAvg[$i] = %0.6f\n",$ResAvg[$i]); printf ("LengthAvg[$i] = %0.6f\n",$LengthAvg[$i]); } }
All of my attempts to create a multi-key hash have not worked out so well. How would a real expert clean up the above? Note that I can't use $NetFanout as a lone key--I have about 1.8 million total table entries, but less than 50 $NetFanout values, hence I use $NetName to make the key unique.
Thanks,
-fiddler42
In reply to Managing multi-key hash tables.... by fiddler42
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |