Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

outputing specific co-ordinates

by Taylorswift13 (Novice)
on Nov 19, 2011 at 08:34 UTC ( #938950=perlquestion: print w/ replies, xml ) Need Help??
Taylorswift13 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i am trying to adapt a code from last week to give me all co-ordinates above a specific threshold in this case 500

#!/usr/bin/perl use Modern::Perl; #does an implict use strict; use warnings and activa +tes "new" features no warnings qw /uninitialized/; my @result; { open my $INPUTFILE, '<', '/Users/ts/input.txt' or die "File error: + input file: $!"; while (<$INPUTFILE>) { chomp; my ($index, $value) = split /\t/; $result[$index] = $value; } } { open my $OUTPUTFILE, '>', '/Users/ts/output.txt' or die "File erro +r: output file: $!"; while (my ($index, $value) = each @result) { next if $value > 500; say $OUTPUTFILE, "$index\t$value"; } } input.txt 1 10 2 11 3 9 4 500 5 550 etc

i am trying to create an output which will give me a file with all values above 500 for example currently it is not generating anything at all in output.txt and just shows all values in terminal and not just those co ordinates above 500 i can't see what i am doing wrong thank you for your time

Comment on outputing specific co-ordinates
Download Code
Re: outputing specific co-ordinates
by zwon (Monsignor) on Nov 19, 2011 at 09:01 UTC
    no warnings qw /uninitialized/;

    I guess you've put it specially to silence the exact warning that tells you where the error is. You're splitting on "\t", but looking onto your input I have the impression that there are no tabs. So $value in

    next if $value > 500;
    is probably uninitialized.

    Update: well, after looking more closely I think real input contains tabs, but then it should work (to some extent). I think it is was a rather weird idea to keep coordinates in array like you did, e.g. you don't have 0 element, and some others may be missing. Here's a simplified version:

    #!/usr/bin/perl use Modern::Perl; while (<DATA>) { chomp; my ($index, $value) = split /\s/; say "$index\t$value" if $value <= 500; } __DATA__ 1 10 2 11 3 9 4 500 5 550

    Update: removed unused var

      Excellent works great and i understand it! thanks

Re: outputing specific co-ordinates
by Anonymous Monk on Nov 19, 2011 at 10:15 UTC
    Comma.
    say $OUTPUTFILE, "$index\t$value";
Re: outputing specific co-ordinates
by Khen1950fx (Canon) on Nov 19, 2011 at 12:01 UTC

    There's no need to use no warnings 'uninitialized';---just initialize the array. Remember, too, that the 1st arg to each must be a hash.

    I tried your script in a slightly different way. I used a hash because I wanted to keep each.
    #!/usr/perl/bin/perl use Modern::Perl; $|=1; my($data) = '/root/Desktop/inputfile.txt'; open STDIN, '<', $data or die "File error: input file: $!"; open STDOUT, '>', '/root/Desktop/output.log' or die "File error: output file: $!"; my(%result) = (); while (defined($_ = <STDIN>)) { chomp $_; my ($index, $value) = split( /\s/, $_, 0 ); $result{$index} = $value; } while (my ($index, $value) = each(%result)) { do { say "$index => $value" if $value > 500; }; } close STDIN; close STDOUT;
    Outputs:
    5 => 550

      Many thanks for your response I'm just wondering how this differs from the first script will i get the same output?

        sorry forgot to log in!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-07-22 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (113 votes), past polls