Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Hash Help

by mmittiga17 (Scribe)
on Nov 25, 2008 at 18:30 UTC ( [id://725910]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash Help
in thread Hash Help

Thanks again for your help, I have a question if you have time, I notice in some cases in the file I am trying to parse that there maybe 7 lines of info per ID and in other cases only 5.
my %records; while (! eof (DATA)) { my $line1 = <DATA>; next unless defined $line1 and $line1 =~ /^01KV/; $_ = <DATA> for my ($line2, $line3, $line4, $line5, $line6, $line7 +); my $id = substr $line1, 2, 9; $records{$id} = [$line1, $line2, $line3, $line4, $line5, $line6, $l +ine7];
If I add line6 and line7, I notice that IDs with only five lines will also grab the first two lines of the next record. How can I dynamically account for IDs with more than 5 record lines? Thanks!

Replies are listed 'Best First'.
Re^3: Hash Help
by GrandFather (Saint) on Nov 25, 2008 at 20:20 UTC

    In that case you need to be smarter about recognizing records. There seems to be a line number associated with each line of a record so you can notice when the line number resets:

    use warnings; use strict; use Data::Dump::Streamer; my %records; my @lines; while (! eof (DATA) or @lines) { my $line = <DATA>; $line ||= ''; # Avoid a bunch of defined tests chomp $line; next unless $line =~ /^([\s\d]\d)/ or @lines; if (! defined $1 or $1 <= @lines) { # Start of new record (or last record) - save previous my $id = substr $lines[0], 9, 5; $records{$id} = [@lines]; @lines = (); } push @lines, $line if length $line; } Dump (\%records); __DATA__ 10101ABC000019101L0001374686047S30339 GA &DOE C080229CR7 7 +00244 0000001 000000000 2 CR7 000 060714Q + Y 0000000000 000 3 00030339 3JO +HN DOE 36 423 MAIN STREET ATLANTA GA 30339 5 +000000000 +0080226I 052461 05241961 6Additional line 1 7and another additional line 10101ABC000029102 N D +3658 MAIN STREET 2 ATLANTA GA3033 +9 0001 3JOHN DOE 05241961INDV37468604 +7S 4 5

    Perl reduces RSI - it saves typing
      with this new method how can I access each ID records lines 1 - X ? Before I could do it this way:
      #########REC01############## $line = $records{$id}[0]; $ACTNUM = substr $line, 2, 9 ; $TOT_AVBL_TO_PAY1 = substr $line, 32, 11; $TOT_AVBL_TO_PAY2 = substr $line, 43, 2; $TOT_AVBL_TO_PAY1 ="0" if (! $TOT_AVBL_TO_PAY1); $TOT_AVBL_TO_PAY2 ="0" if (! $TOT_AVBL_TO_PAY2); $TOT_AVBL_TO_PAY3 = join('.', $TOT_AVBL_TO_PAY1, $TOT_AVBL_TO_PAY2); $TOT_AVBL_TO_PAY = $TOT_AVBL_TO_PAY3; #########REC02############## $line = $records{$id}[1]; #########REC03############## $line = $records{$id}[2]; $MGN_FED_CALL_TOT1 = substr $line, 31, 11; $MGN_FED_CALL_TOT2 = substr $line, 42, 2; $MGN_FED_CALL_TOT1 ="0" if (! $MGN_FED_CALL_TOT1); $MGN_FED_CALL_TOT2 ="0" if (! $MGN_FED_CALL_TOT2); $MGN_FED_CALL_TOT3 = join('.', $MGN_FED_CALL_TOT1, $MGN_FED_CALL_TOT +2); $MGN_FEDSIG = substr $line, 30, 1; if ($MGN_FEDSIG eq "+") { $MGN_FED_CALL_TOT = "-" . $MGN_FED_CALL_TOT3 ; }else{ $MGN_FED_CALL_TOT = $MGN_FED_CALL_TOT3; }
      Thanks!!!!

        Look at the code. Think about the code. Think about the data structure. Examine the result from Dump. You have the answer already and it is time for you to do some thinking and just a little work. You can't expect to be spoon fed answers to every problem you have forever.

        perllol and perlref will help.


        Perl reduces RSI - it saves typing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://725910]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found