It seems I get to do less and less coding these days and as a result I'm have a very hard time figuring out this problem.
I need to read a file in to a hash of hashes, do some stuff and print the data out. There's more to it than that but this is where I'm stuck now. The script will be opening a file delimited with "^" and based on product key it will store the data. Given my code below I'm ending up with only one hash left, and I just can't get the structure in right.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my %mdata;
# using <DATA> for now
open FI, "<", "f:\\cyclecount.txt" or die "open failed $!"; # get file
+ from ini
while (<DATA>) {
chomp;
next unless (s/\^/\^/g == 4);
my @rec = split /\^/;
# $mdata{PKEY} = $rec[0];
my $pkey = trim_trailing($rec[0]);
%mdata = (
$pkey => {
ABC => trim_trailing($rec[1]),
DESC => trim_trailing($rec[2]),
UNIT => trim_trailing($rec[3]),
LOC => trim_trailing($rec[4])
}
)
}
close FI or warn $!;
print Dumper \%mdata;
sub trim_trailing {
my $s = shift;
$s =~ s/\s+$//;
return $s
}
__DATA__
0V704-B ^B^STERILE OR TOWELS ^CS/20 EA ^A/2/1
0ZS-050 ^B^ZERO WET SPLASHIELD ^EA ^H/2/1
00E7507 ^B^POLYHESIVE II ^CS/50 EA ^H/3/3
00ER320 ^A^APPLIER MULTIPLE CLIP MED/LG ^BX/3 EA ^G/6/4
00H-334 ^B^SUCTION POLY TRAP ^EA ^G/4/5
000V400 ^B^LAP SPONGES ^CS/40 EA ^A/2/1
I should have 6 records but Data::Dumper shows I only have the last:
$VAR1 = {
'000V400' => {
'UNIT' => 'CS/40 EA',
'DESC' => 'LAP SPONGES',
'ABC' => 'B',
'LOC' => 'A/2/1'
}
};
I'm sure I'm missing something obvious, but its been several years since I've done regular coding and months since I've done any perl.