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

Array from list in input file?

by SandraA (Initiate)
on Nov 24, 2015 at 21:13 UTC ( [id://1148549]=perlquestion: print w/replies, xml ) Need Help??

SandraA has asked for the wisdom of the Perl Monks concerning the following question:

I have a file containing three columns of strings (the last one is a numeric value) I want a code that prints the string in the first column for every line where the third value is above a certain threshold. My first idea was to open the file and treat the values as an Array with Three scalars, split the scalars and ask perl to print the first one if the third one was larger than my threshold. I don't know if that is a good way to do it, and anyway I have no idea how to write it. Could anyone help me solve my problem?

Replies are listed 'Best First'.
Re: Array from list in input file?
by toolic (Bishop) on Nov 24, 2015 at 21:21 UTC
    Sounds like a good approach.

    • Read perlintro
    • Write some code.
    • Post back here if you have specific questions about your code.
      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?

        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,

Re: Array from list in input file?
by muba (Priest) on Nov 24, 2015 at 22:39 UTC

    Sounds like a dang fine approach to me.

    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found