Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Removing lines ending with integers larger than x

by dtbach (Novice)
on Mar 13, 2012 at 12:28 UTC ( #959313=perlquestion: print w/replies, xml ) Need Help??
dtbach has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Perl Monks and followers: I've been using a convoluted method to find all lines in a file that end with 47 or greater, but would appreciate feedback how to accomplish this using perl. The format of the data is similar to the following: 20120312-16:00:02,39 so this line would be included in final output. Just want to parse content then delete all lines which are >=47 and save file as same name. Thank you to anyone who replies. Darryl
  • Comment on Removing lines ending with integers larger than x

Replies are listed 'Best First'.
Re: Removing lines ending with integers larger than x
by moritz (Cardinal) on Mar 13, 2012 at 12:30 UTC
Re: Removing lines ending with integers larger than x
by sundialsvc4 (Abbot) on Mar 13, 2012 at 12:58 UTC

    The first question would be to see if the file format you are dealing with is something that is already understood by a module at   (It may well be that your entire requirement has already been solved, and if so, switch your attention from building something, to finding it.)

    If not, you need to use a regular expression such as something like this ... /\,([0-9]+)$/ ... which will locate lines ending with ("$") a literal comma ("\,") followed by group of one or more ("+") digits ("[0-9]"), and which will return the contents of that group to you as a separate variable ("$1") that you can now examine.

    Since you want to ensure a numeric comparison, adding zero to it is an easy way ... my $value = $1 + 0; ... and then you simply see if it is 47 or greater.   One common way to do that, in a loop, is the slightly-different but very succinct, next unless $value >= 47;.

    (Careful, now!   It is very easy to write what is not-obviously a string-based comparison that compiles, and that superficially appears to solve the problem in your test cases (if you actually ran any...) but that in fact is not telling the computer to do what you want.   Sux...)

    I hope that this generalized description of the idea ... helps.

Re: Removing lines ending with integers larger than x
by thunders (Priest) on Mar 13, 2012 at 15:08 UTC
    The one liners above are fine, but if I needed to incorporate this into a larger script, I would write the filtered data to a new file, then replace the original with the new file.
    use strict; use IO::File; #open the original report for reading my $file = IO::File->new("data.txt","r") or die($!); #Create a new file to write to my $filtered_file = IO::File->new("data_filtered.txt","w") or die($!); #read report file while ( my $line = <$file> ) { #remove newline chomp($line); my ( $data, $number ) = split ",", $line; $filtered_file->print("$line\n") if $number < 47; } $file->close; $filtered_file->close; # Replace the original file rename("data_filtered.txt","data.txt");
Re: Removing lines ending with integers larger than x
by choroba (Chancellor) on Mar 13, 2012 at 13:31 UTC
    You method must truly be convoluted if 39 is considered 47 or greater :-)
      The 39 example would be included in final output as it is less than 47, maybe it wasn't worded properly. Thanks for the feedback, gave me some points to consider. Darryl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://959313]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-08-18 19:11 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (309 votes). Check out past polls.