in reply to Multidimensional array of hashes
Other monks have shown you ways of dealing with this specific problem. I'd like to comment on your node title, multidimensional arrays of hashes. Perl's system of handling such things using references is really cool, IMO. Because the elements of an array or the values of a hash can hold references, and because a reference can point to an array or a hash, it is possible to nest these structures to arbitrary depth, creating such things as, as you put it, multidimensional arrays of hashes. That's not the cool part. The cool part is that when you put the array and hash subscripts back to back, the dereferencing is automatically implied. Thus, you can just do things like this:
$db{BB}{industry} = "Drinks Industry"; $db{BB}{supplier}{BB100} = "Coffee Suppliers";
The above does exactly what you would want it to do. (In practice, rather than filling in individual values as above, you could fill in the data while reading your input file, or something like it.) Now, you want a list of all the industries? No problem...
my @industrycodes = sort keys %db; foreach $i (@industrycodes) { print "$i => $db{$i}{industry}\n"; }
And if you also want the suppliers? Again, no problem...
my @industrycodes = sort keys %db; foreach $i (@industrycodes) { print "Suppliers in the $db{$i}{industry}:\n"; my %s = %$db{$i}{suppliers}; # Note that: We wanted the hash of suppliers, # so we had to dereference with the hash sigil, % # The dereferencing of the references used for the # nesting is all handled automatically by the # subscripting syntax, so you don't have to mess # with it, but when you want to retrieve something # other than a scalar (e.g, an array or hash), # you do have to dereference the retrieved result. foreach $supplier (sort keys %s) { print "\t$supplier => $s{$supplier}\n"; } }
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|
|---|