in reply to Stuck while learning about the hash

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 '#'