I'm attempting to learn how to use hashes and I'm stuck. I am trying to parse the following file:
614 MNH,16.00|USED,32.00|PB,50.00
615 MNH,36.00|USED,12.00|PB,50.00
616 MNH,96.00|USED,2.00|PB,10.00
The code I am using to do so is as follows:
#!/usr/bin/perl -w
use strict;
my (@record, @details, @item, $detail, $scottnum, $details, %items);
open(DB, "db.txt") ||
die "Could not open the database: $!";
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{"$item[0]"} = "$item[1]";
foreach my $key (%items) {
print "$scottnum\t $key \t $items{$
+key}\n";
}
}
}
}
close(DB) ||
die "Could not close the database: $!";
I keep getting the error: "use of uninitialized value at parse.pl line 19, <DB> chunk 3". I get that error every other line. Could someone please explain to me what I'm doing wrong?
Stamp_Guy
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.