Sounds like a good approach.
- Read perlintro
- Write some code.
- Post back here if you have specific questions about your code.
| [reply] |
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?
| [reply] |
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:
- 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.
- 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,
| [reply] [d/l] [select] |
use strict;
use warnings;
use feature 'say';
my $minimum = 100; # The threshold.
while(<DATA>) { # Read it
my ($str, undef, $nr) = split / /; # Split it
say $str if $nr >= $minimum; # Say it
}
__DATA__
Too low 1
Also nope 2
Just fine 100
Not there 3
Hidden hideaway 4
Another one 101
Hiding quierly 5
Also no 6
Perl rocks! 102
Again no 7
Nope nopenope 8
Hacker hackityhack 103
Cracker crackitycrack 9
Hater hatitiyhate 10
Just
Another
Perl
Hacker
It even works, too!
| [reply] [d/l] [select] |