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$/

In reply to Re: Multidimensional array of hashes by jonadab
in thread Multidimensional array of hashes by robbiebow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.