Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Regex Not Grabbing Everything

by japhy (Canon)
on Sep 17, 2010 at 15:19 UTC ( [id://860506]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regex Not Grabbing Everything
in thread Regex Not Grabbing Everything

I also told you that the string '0.00' is only FOUR characters, and you were taking a substring of FIVE characters.

Your code is written in a rather confusing manner. You've got code in loops that shouldn't be there. What you want to do is keep all the lines until you find one that matches your criteria, and then print the lines you've kept and all the lines following it. Here is a sample solution:
# print all lines from 'START' to 'STOP' # if a line in between them has 'foobar' at position 10 my $target = 'foobar'; my $pos = 10; my (@buffer, $found); while (<FILE>) { if (/START/ .. /STOP/) { # if we have already found our target string, print this line if ($found) { print } # otherwise... else { # store this line in our buffer push @buffer, $_; # and if we find the target string at the right location # set $found to 1, and print the buffer if (substr($_, $pos, length($target)) eq $target) { $found = 1; print @buffer; } } } }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
Nos autem praedicamus Christum crucifixum (1 Cor. 1:23) - The Cross Reference (My Blog)

Replies are listed 'Best First'.
Re^4: Regex Not Grabbing Everything
by JonDepp (Novice) on Sep 17, 2010 at 16:24 UTC

    There is a space for numbers that are five characters in the substring. I changed the 5 to 4 and got 0.0. I used your code and got an empty output file. The first IF statement couldn't have a found in there because the condition that makes the statement true is in the substring deeper in the code. My original code returns what I want in the order I need it but stops before the end of the regex. I commented out different parts of the code to see if the array I was pushing $_ to from the regex was capturing everything, and it is. The code prints out everything I need up until I check for the 0.00 condition. After that it prints out if the 0.00 condition is true but truncates the array before the "ADJ TO TOTALS" line. Is there a reason why testing for that substring condition would truncate the array?

      If changing the 5 to 4 yields '0.0', then I think it's actually yielding ' 0.0' (that's space-zero-dot-zero), which means your offset of 118 might be wrong.

      The reason your code is failing, in general, is because your logic is faulty. You think your code is saying "print the contents of the array if there's a line with '0.00' at offset 118", but your code is NOT saying that.

      Your code is saying:

      for each line in between 'NAME' and 'ADJ TO TOTALS:', do this { add this line to the @data array now go through each element of the @data array, and do this { # <-- + WHY?! if this line has 1235114182 in it, do this { put this line at the end of $lines if $lines has '0.00' at offset 118, do this { print the @data array } empty the @data array, $lines, etc. } } }
      Your code is faulty because it won't print any line that DOESN'T have '0.00' in it.

      I suggest you try my sample solution and plug in the values for $target, $pos, 'START', and 'END'.


      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      Nos autem praedicamus Christum crucifixum (1 Cor. 1:23) - The Cross Reference (My Blog)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found