Ignoring the error, your code is logically very clear. It follows the database layout exactly.

I'd like to point out a couple of hash tricks which will simplify the code and make it easier to extend.

Unless your print statement is a proxy for an sql insert or a print to a different flat file, you may want to collect all the data in an uberhash. That might not be desirable if the database is large.

#!/usr/bin/perl -w use strict; my $records = {}; # reference to the anonymous uberhash my @record; #could be declared my in the loop where assigned open(DB, "< db.txt") || die "Could not open the database: $!"; while(<DB>) { chomp; @record = split(/\t/); $records->{$record[0]} = { # we're starting an anon hashref split /\||,/, # split to list on pipe or comma $record[1]}; # done print map "$record[0]\t$_\t$records->{ $record[0] }{ $_ }\n", keys %{$records->{$record[0]}}; } close(DB) || die "Could not close the database: $!"; # now you can set other values in the hash if you like: # $records->{$index}{'description'} = "Randomly indexed item";

By doing the item hash in one swell foop, we save on temporaries and loop accounting. Separating the printed output from the db parsing makes it easier to cut and paste on the code.

After Compline,
Zaxo

UpdateCorrected missing '#'


In reply to Re: Stuck while learning about the hash by Zaxo
in thread Stuck while learning about the hash by Stamp_Guy

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.