You can use a single hash to store all of this:
open CATEGORY, "categories.txt"; # ...or whatever... foreach $line ( <CATEGORY> ) { @ITEMS = split /\|/, $line; foreach $item ( @ITEMS ) { ($value, $key) = split /,/, $item; $LIST{$key} = $value; } } close CATEGORY; open DESC, "descriptions.txt"; # Same code as above, except key/value pairs are reversed. foreach $line ( <DESC> ) { @ITEMS = split /\|/, $line; foreach $item ( @ITEMS ) { ($key, $value) = split /,/, $item; $LIST{$key} = $value; } } close DESC; # Now arrange for the output. open OUTFILE, ">output.txt"; foreach $reference ( sort keys %LIST ) { # Sometimes, it's a new category. These are most important. if ( $reference =~ /^\'\w\w\'$/ ) { print OUTFILE "\n"; } else { print OUTFILE "|"; } print OUTFILE "$reference,$LIST{$reference}"; }

That should do it. Since they're sorted, your category references will always precede your item references. Since the code above checks for that first, you'll start the new line just in time to deal with that item.

Every printed item is preceded by the delimiter it needs. Newlines for a new category, pipes for a new item. That way you don't get extra trailing separators.

There's no error checking code in here, though. If your two input files aren't pristine, you'll get some junk in there. Also, your category references *must* be two alpha characters. If you change that, you might need to change the pattern for recognizing a new category to something more flexible like simply ruling out an item:

if ( $reference !~ /^\'\w+\d+\'$/ ) { # ... }

If it isn't an item, maybe it's a category. :-)

Hope that helps!

--Rhys

edited: Sat Oct 4 12:59:29 2003 by jeffa - s/pre/code/ig


In reply to Re: Multidimensional array of hashes by Rhys
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.