But this probably isn't what you wanted. You probably wanted a hash with a key and a subkey (a hash of hashes). I'm not good at explaining so let me show you instead:if (($item[0] ne "") && ($item[1] ne "")){ $items{"$item[0]"} = "$item[1]"; foreach my $key (keys %items) { print "$scottnum\t $key \t $items{$key}\n"; } undef %items; }
This way a hash of hashes is created.while(<DB>) { chomp; @record = split(/\t/); $scottnum = $record[0]; @details = split(/\|/, "$record[1]"); foreach $detail (@details) { @item = split(/,/, "$detail"); if (($item[0] ne "") && ($item[1] ne "")){ ########################################### $items{$scottnum}{"$item[0]"} = "$item[1]"; ########################################### } } } foreach my $scottnum (keys %items) { for my $key (keys %{$items{$scottnum}}) { print "$scottnum\t $key \t $items{$scottnum}{$key}\n"; } }
Stylistically, I would have probably done it something like:
But you are definately on the right track!my %info; open(DB, "db.txt") || die "Could not open the database: $!"; while (<DB>) { my @info=split /[\s|,]+/,$_; my $scott=shift @info; while (my $key=shift @info) { $info{$scott}{$key}=shift @info; } } close DB;
In reply to Re: Re: Re: Stuck while learning about the hash
by mr.nick
in thread Stuck while learning about the hash
by Stamp_Guy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |