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

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

  • Comment on Re^4: Multiply the numbers in a text file

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

    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.