The code others have posted above is good, so I'll simply go into a little more depth about what it does.

Once you've got $artist, $album, and $song for a given line, you're going to want to put them into a nested hash.

Assume %songs is your "main" hash. What it's going to contain is references to "anonymous" hashes, one for each artist, keyed by the name of the artist. Each of those hashes will contain references to hashes for each album, keyed by album title. Each album hash will contain, keyed by the name of the song, some piece of information about the song, such as it's filename. If you don't want to keep any information about the song, you can use an array for the innermost element, instead.

To access an individual element, look it up as

$songs{"artistname"}->{"albumname"}->{"songname"} #[----] # %songs is a hash #[------------------] # each value in %songs is a reference, to a hash or "hashref". #[--------------------] # the "->" dereferences, so we've got a hash to look up # "albumname" in. #[---------------------------------] # the look up in the inner hash returns another hashref #[-----------------------------------] # so we dereference again, and we're dealing with another # hash... #[----------------------------------------------] # ...in which we look up "songname", getting whatever.
(The ->'s are optional between '}' and '{', by the way.)

To print the whole thing out again, simply use nested foreach loops to iterate through each element.

see perlref for more details, or the second half of Chapter 4 of the Camel Book

--
Ryan Koppenhaver, Aspiring Perl Hacker
"I ask for so little. Just fear me, love me, do as I say and I will be your slave."

Update:Sorry, I removed the mangled HTML. I had commented something out, and it took out half the page with it. I feel especially dumb, having done this once already on E2


In reply to Re: Hashes hunt me even when I sleep.... by rlk
in thread Hashes hunt me even when I sleep.... by bman

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.