in reply to Re^8: Easiest way to filter a file based on user input (updated)
in thread Easiest way to filter a file based on user input

I added the line $limit = abs($limit); (see abs) because I wasn't sure of your original specification, as I asked in my post. Also, note that -places=N is documented as: "the number is assumed to have exactly N places after the radix point" and even goes on to show an example: "$RE{num}{real}{-places=>2} # matches 123.45 or -0.12", and your input isn't in that format. Take some time to look into the documentation and then try removing the line with abs, as well as "{-places=>2}" from the regex.

Replies are listed 'Best First'.
Re^10: Easiest way to filter a file based on user input
by Peter Keystrokes (Beadle) on Jul 16, 2017 at 13:14 UTC

    Oh okay, apologies for the buffoonery on my part.

    The script seems to be working fine now, I added another next line: next if /^(\s\s-\d)/ && $1 > $limit;, because without it, it doesn't recognise regular Real numbers like -2, -5 etc.

    The script:

    #!/usr/bin/perl use strict; use warnings; use Regexp::Common qw /number/; print "Enter limit: "; chomp( my $limit = <STDIN> ); #$limit = abs($limit); open my $IN, '<', "xt_spacer_results.hairpin" or die $!; open my $SIFTED, '>', "new_xt_spacer_results.hairpin" or die $!; while (<$IN>){ next if /^None/; next if /^($RE{num}{real})/ && $1 > $limit; next if /^(\s\s-\d)/ && $1 > $limit; print $SIFTED $_; } close $IN; close $SIFTED;

    Haukex, you are a legend, thanks.

      it doesn't recognise regular Real numbers like -2, -5

      It does, here's a way you can test that (see e.g. How to ask better questions using Test::More and sample data):

      use warnings; use strict; use Test::More; use Regexp::Common qw/number/; like "-2", qr/^$RE{num}{real}$/; like "-5", qr/^$RE{num}{real}$/; like " -5", qr/^$RE{num}{real}$/; done_testing; __END__ ok 1 ok 2 not ok 3 # Failed test at ... 1..3 # Looks like you failed 1 test of 3.

      As you can see, the problem isn't that it doesn't match integers, it's the whitespace at the beginning of the line. Try changing

      next if /^($RE{num}{real})/ && $1 > $limit; next if /^(\s\s-\d)/ && $1 > $limit;

      to

      next if /^\s*($RE{num}{real})/ && $1 > $limit;

      Where \s* means "zero or more whitespace characters" (perlretut).

        Oooh thanks!