Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Array from list in input file?

by SandraA (Initiate)
on Dec 01, 2015 at 12:57 UTC ( [id://1149038]=note: print w/replies, xml ) Need Help??


in reply to Re: Array from list in input file?
in thread Array from list in input file?

I was thinking something like this #!/usr/bin/perl -w use strict; my $infile = $ARGV[0]; open (IN, $infile); my $threshold = 1e-50; while (my $line = <IN>) { my @linearray = split("\t", $line); if($linearray2 <$threshold) { print "$linearray[0]\n"; } } Would that work or is there some smarter and better way?

Replies are listed 'Best First'.
Re^3: Array from list in input file?
by Athanasius (Archbishop) on Dec 01, 2015 at 14:30 UTC

    Hello SandraA, and welcome to the Monastery!

    Please enclose your code in <code> ... </code> tags, that will make it much easier to read (and $linearray[2] will display correctly).

    Your approach is essentially the same as that given by muba, and it should work fine, except:

    1. You are printing the line only if the third field is less than the threshold, but your original specification was: to print the first one if the third one was larger than my threshold (underlining added). See perlop#Relational-Operators.
    2. You are splitting on "\t", which is fine as long as the data fields are always separated by single tabs. But splitting on whitespace, using /\s+/ or the equivalent ' ', is probably safer. See split.
    ...is there some smarter and better way?

    Well, if you’re into one-liners, then (assuming your data is in, say, a file called “data.txt” in the current directory) you can do this:

    >perl -anE "BEGIN { $threshold = shift; } say $F[0] if $F[2] > $thresh +old;" 1e-50 data.txt

    The -a, -n, and -E switches are explained in perlrun. But code stored in a file is, IMO, easier to debug, reuse, and maintain.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found