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

Re^2: extracting columns

by perllearner007 (Acolyte)
on Mar 22, 2012 at 20:24 UTC ( [id://961094]=note: print w/replies, xml ) Need Help??


in reply to Re: extracting columns
in thread extracting columns

Hi Marshall, Thanks for the reply. However, changing to my @fields = split /\t/,$_;hasn't resolved the issue. I still get the same output. Thanks for pointing out the <\code> error!

Replies are listed 'Best First'.
Re^3: extracting columns
by Marshall (Canon) on Mar 22, 2012 at 22:41 UTC
    Thanks for the code tag change! Now I can read it.
    Evidently, you do not need the 2nd arg in the split(), my memory was faulty, Ooops.

    When you add in the $SIZE comparison, you need to do something about the line that is not numeric (the first header line) - otherwise the comparison will fail with "$SIZE not numeric". You could read and deal with that first line before the loop or below I added a test in the "if" to check that it is numeric before doing the comparison. In the "if" if $SIZE doesn't start with 0-9 or the "minus sign" or period, then rest of the "if" is skipped and the line is printed.

    I usually add a "skip blank" line statement in these things as sometimes there is a trailing blank line that causes trouble- that's purely optional.

    #!usr/bin/perl -w use strict; while(<DATA>) { next if /^\s$/; # skip blank lines my ($SAMPLE_NAME, $SIZE) = (split /\s+/)[1,3]; # print line if $SIZE isn't numeric, e.g. "SIZE" # or if it is a number, then must meet min, max criteria if ($SIZE =~ /^[^0-9-.]/ or ( $SIZE > 2 and $SIZE <5)) { print "$SAMPLE_NAME $SIZE\n"; } } =prints SAMPLE_NAME SIZE U7345 4.333 =cut __DATA__ ID SAMPLE_NAME EFFECTS SIZE 001 U7654 NEGATIVE 5.666 002 U7345 POSITIVE 4.333 003 U7674 NEGATIVE 1.696 002 U7845 POSITIVE -4.333

      Thanks Marshall. I do get the extracted columns but it gives me all the values even after I set the size limitation.

      Thank you! There was an error in the data! It is resolved now!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found