in reply to Hashes hunt me even when I sleep....
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
(The ->'s are optional between '}' and '{', by the way.)$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.
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
|
|---|