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.

Sweetblood


In reply to Reading data into a hash of hashes. by sweetblood

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.