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

Re^3: Multiply the numbers in a text file

by aaron_baugher (Curate)
on May 10, 2015 at 06:34 UTC ( [id://1126235]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Multiply the numbers in a text file
in thread Multiply the numbers in a text file

Now that you've provided some sample data, we can accomplish something. You still need to make your requirements more concrete, though. What qualifies as a "small negative value"? All negative values, or ones below a certain value? If these should be converted to "0 or -9999", which should it choose? If some should be 0 and some should be -9999, based on what criteria? If you hired me to write this script, I would need you to answer those questions.

To get you started, you can skip the first six lines by simply reading and writing them:

for (1..6){ $line = <$input_file>; print $output_file $line; }

Then proceed with your filtering on the remaining lines. I'll guess that you want all negative values smaller than -0.1 replaced with 0 or -9999 alternatively, assume that values are space-delimited, and continue:

my $M = 5; # constant multiplier my $T = 0.1 # negative number threshold my $al = 0; # alternator while(<$input_file>){ s|([-.\d]+)| if($1 == -9999){ $1; # leave -9999 alone } elsif( $1 < 0 and abs($1) < $T ){ $al++ % 2 ? 0 : -9999; # replace small negative with 0 or -999 +9 } else { $1 * $M; # multiply other numbers by constant } |ge; print $output_file; }

If you want your changes to replace the original file, the best way to do that is to write to a new file and then copy it over the old file when you're finished. You can do that copy manually, or make that part of your script after you've tested enough to be confident that it will work correctly.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Replies are listed 'Best First'.
Re^4: Multiply the numbers in a text file
by zegoofer (Initiate) on May 10, 2015 at 14:26 UTC

    Any value less than zero will be considered as a negative number except -9999, because that represents the no data value.

    Thanks for your help Aaron. I'm trying to get the code running

      After trying out your code, I get the output which I'm unable to interpret

      OB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)

      It looks something like this

      It doesn't write in the output file except for the first six lines. Can you tell me what the error is?

        #!/usr/bin/perl use strict; use warnings; my $input = 'input.txt'; my $output = 'output.txt'; my $constant = 15; #number you are multiplying by my $minimum = -0.0001; # Zeroize numbers smaller than this my $NO_DATA = -9999; open my $in, "<", $input or die "Could not open $input for reading:$!" +; open my $out, ">>", $output or die "Could not open $output for append: +$!"; while (<$in>){ next if $. < 7; # Skip first 6 lines chomp $_; my @values = split; for (@values){ if($_ == $NO_DATA){ # leave -9999 alone } elsif( $_ < $minimum ){ $_ = 0; # or $NO_DATA; # replace small negative with 0 or -999 +9 } else { $_ *= $constant; # multiply other numbers by constant } } print $out join ( " ",@values),"\n"; } close $out; close $in;

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

        The code I showed was not complete; it was just portions to demonstrate how to do the number changes. It's customary to show your code that you need help with, so we can point out what's causing the errors.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found