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

Re: extracting columns

by Marshall (Canon)
on Mar 22, 2012 at 18:38 UTC ( [id://961071]=note: print w/replies, xml ) Need Help??


in reply to extracting columns

When you do something other than the default split, you need to specify the variable to split upon.
my @fields = split /\t/; should be: my @fields = split /\t/,$_;
Add tags around code so that it appears formatted correctly in the post. <code>...your code here</code>
Oh, I see <\code> should be </code>

before the print, just skip the print and loop for next line if $SIZE doesn't meet the criteria:

next unless ($SIZE >2.0 and $SIZE < 5.0);

Replies are listed 'Best First'.
Re^2: extracting columns
by perllearner007 (Acolyte) on Mar 22, 2012 at 20:24 UTC
    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!
      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://961071]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found