in reply to Re: Easiest way to filter a file based on user input
in thread Easiest way to filter a file based on user input

My sincerest apologies for the amateurish code you're about to see...
#!/usr/bin/perl use strict; use warnings; print "The lower the score the more stable the structure.", "\n", "Please set a limiting value e.g. -3: ", "\n"; my $value = <STDIN>; open IN, "file.hairpin", or die $!; my @trash; my @treasure; while (<IN>){ if ($_ =~ /^>+/){ push @treasure, $_; }elsif($_ =~ /^None+/){ push @trash, $_; }elsif($_ =~ /(^d+)/){ ## Here I don't know how to incorporate the value I get from the us +er with the value ## in the file }else{ push @treasure, $_; } } close IN; foreach my $stuff (@treasure){ open SIFTED, '>>', "new_file.hairpin", or die $!; print SIFTED, $stuff."\n"; close SIFTED; }

Replies are listed 'Best First'.
Re^3: Easiest way to filter a file based on user input
by 1nickt (Canon) on Jul 07, 2017 at 12:28 UTC

    Hi, thanks for posting your code.

    Here is a version that appears to do what you want. Note the following things:

    • You need to chomp() the user input to remove the newline so you can use the string in a comparison.
    • The sample data you provided doesn't contain anything that would match your first regexp.
    • The regexp for matching the start of the line with the user input crudely and *only* matches negative numbers with exactly one integer and one decimal place. You'll need to change it if the user could enter a positive number, or a negative integer, or anything else.
    • After capturing the match it is available in the special variable $1, which is used for the comparison.
    • I placed your sample data in the script in the __DATA__ section for this demo; it's fine to open and read a file as in your original. I also skipped the writing to an out file.
    • I placed multiple "debug statements" in the code, i.e. printing out things to show what's going on. Once the program is working correctly you can remove those, but it's a good technique for discovering problems in your data processing.
    #!/usr/bin/perl use strict; use warnings; use feature 'say'; print "The lower the score the more stable the structure.", "\n", "Please set a limiting value e.g. -3: ", "\n"; chomp( my $value = <STDIN> ); chomp( my @input = <DATA> ); my @trash; my @treasure; for ( @input ){ if ( /^>+/ ) { say "$_ matches '/^>+/'"; push @treasure, $_; } elsif ( /^None/ ) { say "$_ matches '/^None/'"; push @trash, $_; } elsif( /(^[\d\.-]{4})/ ) { say "$_ matches '/(^[\d\.-]{4})/'"; if ( $1 <= $value ) { say "$1 is <= $value"; push @treasure, $_; } else { say "$1 is > $value"; push @trash, $_; } } else { say "$_ doesn't match anything!"; push @trash, $_; } } say 'Treasure:'; foreach my $stuff ( @treasure ) { say $stuff; } __END__ hsa_circ_0067224|chr3:128345575-128345675-|NM_002950|RPN1 FORWARD -4.4.. 6 .. 17 xxxxxxxxxxGTGAC CAGT ATGC ACTG AAGATGAGGTTTGTG -0.9.. 5 .. 18 xxxxxxxxxxxGTGA CCAGT ATGC ACTGA AGATGAGGTTTGTGG None.. 1 .. 20 xxxxxxxxxxxxxxx GTGACCAGTATGCACTGAAG ATGAGGTTTGTGGAC

    Hope this helps!


    The way forward always starts with a minimal test.
      Thank you, it's nice to see some new features I haven't seen before with explanations; I really appreciate this. I'm going to try and implement this now.

      I will get back soon.