Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Moving to next iteration of a while loop

by Jalcock501 (Sexton)
on Sep 23, 2013 at 16:26 UTC ( #1055328=perlquestion: print w/ replies, xml ) Need Help??
Jalcock501 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys

For all intensive purposes the script should work however I have one tiny problem. I cannot seem to skip from one iteration to the next. It just carries on.

use strict; open my $in, '<', 'CLF17936.out'; open my $out, '>', "logfile.txt"; my $check = 0; my @lines; LINE: while(<$in>) { @lines = split('', $_); $check += 1 if $lines[0] eq 'h'; if($check%2 == 1) { SWITCH: { if($lines[0] eq 'j') { my $first = substr $_, 37, 20; my $second = substr $_, 67, 3; print $out "\n Name: $first\t ABICODE: $second\n"; } if($lines[0] eq 'k') { my $qstatus = substr $_, 113, 1; if($qstatus ne "1") { print $out "FAIL\n"; next LINE; } } if(($lines[0] eq 'E') || ($lines[0] eq 'G')) { print $out "$_"; } } } } close($in);
It just needs to miss the last if statement if the above prints to $out as a FAIL. However in the out put file this is what I am getting
Name: B-D Z2 -10 ABICODE: 216 FAIL E99HEADER|004|001| E99INSSCH|216| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRZ01a||||||"00I10587"| E99INSFAC2|MSRZ01b||||||"335000 B"| G3301R7435459
Can anyone advise if its just something I've missed? Thanks Jim

Comment on Moving to next iteration of a while loop
Select or Download Code
Re: Moving to next iteration of a while loop
by hdb (Prior) on Sep 23, 2013 at 16:36 UTC

    The next statement comes after the print. For a meaningful test, it should be the other way round...

    UPDATE: That was stupid from me. What I think is happening, if a line begins with an 'E' or 'G' you never hit the next statement, so the if statement is executed.

Re: Moving to next iteration of a while loop
by trippledubs (Monk) on Sep 23, 2013 at 16:58 UTC
    Hi,

    Possibly @lines could be better expressed as @letters or maybe @characters. The next line after FAIL starts with an E, not a k, so it seems to be working. If it was consecutively going through both the 'k' and 'E|G' if blocks I would expect FAIL and then a line beginning with k.

Re: Moving to next iteration of a while loop
by trippledubs (Monk) on Sep 24, 2013 at 01:25 UTC
    Okay I think I know what you meant. In your earlier Search file for certain lines post the monks told you to use a state machine so you were using $check%2 == 1.

    When it doubt, spell it out! Be more explicit if it helps you think it through

    #!/usr/bin/env perl use strict; use warnings; my $state = 0; sub change_state { my $state = shift; return 1 if $state == 0; return 0 if $state == 1; die "epic fail" } sub read_between_hs { my $line=shift; if ($line =~ /^(j|E|G)/) { print $line; } } while (<DATA>) { my $line = $_; $state = change_state($state) if ($line =~ /^h/); read_between_hs($line) if ($state); } __DATA__ h start printing x dontprintme a dontprintme G printme j printme E printme h stop printing j not between hs should not print E dont print me G dont print me h start printing definitely printable j ahh back to not being ignored G print E print e print -- just kidding h stop printing
Re: Moving to next iteration of a while loop
by mtmcc (Hermit) on Sep 24, 2013 at 10:27 UTC
    For what it's worth, and i might be wrong, I suspect your program doesn't actually do what you want it to do. Unless you really do what to only consider every second record?

    I've modified your data and fail test a little (in the second record) to deliberately make a fail state, rather than counting out to character 113... This does consider every record, and stops processing that data for the record if the fail condition is met...

    #!/usr/bin/perl use strict; use warnings; my @lines; my $status; my $recordTitle; LINE: while(<DATA>) { @lines = split('', $_); $status = 'TEST' if $lines[0] eq 'h'; $recordTitle = $_ if $lines[0] eq 'h'; chomp $recordTitle; if($status eq 'TEST') { if($lines[0] eq 'j') { my $first = substr $_, 37, 20; my $second = substr $_, 67, 3; print STDERR "\nName: $first\tRECORD: $recordT +itle\t ABICODE: $second\n"; } if($lines[0] eq 'k') { my $qstatus = substr $_, 4, 9; if($qstatus eq "TEST FAIL") { print STDERR "FAIL\n"; $status = 'FAIL' } } if(($lines[0] eq 'E') || ($lines[0] eq 'G')) { print STDERR "$_"; } } } __DATA__ h132BIK2 u3*** TEST DATA *** u3*** COMMENT AREA FOR TEST DATA *** j1000010017 6790194100109201301092013Test Data N PW09-3PY248 +018BIK20 k10 2R 1 0045.1011N01010215.820012.220006.0000000 0250M 1I +nsured Only NYY01N00000.00N00000.00Y00000. +00 000215.82000012.22000006.00 q0215.820215.820215.820215.820215.820000000000000000000002500250025002 +500250YY00000 01000215.82000215.82000215.82000215.82000215.82 l02001 0400000000 +0000000000000000000000000000000000000000 a000.00000.00000.0000 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708 yIIDD0043.160019.0110018.9909M0000.000010.000233.08N0017.270023.500000 +43.16000019.01000018.99000000.00000010.00000233.08000017.27 h216BIK0 u3*** TEST DATA *** u3*** COMMENT AREA FOR TEST DATA *** pMU76 Nov 2010 A B C D E F G H + I J L + + 0000000000 j1000010017 6790194100109201301092013Test Data M PW09-3PY248 +005BIK00 k10 TEST FAIL 2R 1 0045.1011N01010217.190012.290006.000000 +0 0250M 1Insured Only NYY01N00000.00N00000 +.00Y00000.00 000217.19000012.29000006.00 q0217.190217.190217.190217.190217.190000000000000000000002500250025002 +500250YY00000 01000217.19000217.19000217.19000217.19000217.19 l02001 0400000000 +0000000000000000000000000000000000000000 a000.00000.00000.0000 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708 yIIDD0043.440019.1410019.1109M0000.000010.000234.57N0017.380023.500000 +43.44000019.14000019.11000000.00000010.00000234.57000017.38 h217BIK1 u3*** TEST DATA *** u3*** COMMENT AREA FOR TEST DATA *** pMU76 Nov 2010 A B C D E F G H + I J L + 0000000000 j1000010017 6790194100109201301092013Test Data L PW09-3PY248 +006BIK10 k10 2R 1 0045.1011N01010222.940012.620006.0000000 0250M 1I +nsured Only NYY01N00000.00N00000.00Y00000. +00 000222.94000012.62000006.00 q0222.940222.940222.940222.940222.940000000000000000000002500250025002 +500250YY00000 01000222.94000222.94000222.94000222.94000222.94 l02001 0400000000 +0000000000000000000000000000000000000000 a000.00000.00000.0000 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708 yIIDD0044.590019.6110019.6209M0000.000010.000240.78N0017.840023.500000 +44.59000019.61000019.62000000.00000010.00000240.78000017.84

    If you really do want to check only every second record, that's a different story...

    I hope that helps!
      Thanks I hadn't considered that I was missing every 2nd record when I check against the data you were right, although I'm sure that's no surprise to your. Thanks for all your help and this is working perfectly now.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055328]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-29 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (190 votes), past polls