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

Adding best practices (Local filehandles, error checking), and efficiency to your code, and adding a zero value check as the OP requested:
#!/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 = 20; # Smallest number to multiply my $zero_replace = 666; # Replacement for zero 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>){ chomp $_; $_ = $zero_replace if 0 == $_; # next if $_ < $minimum; print $out ($_ * $constant),"\n"; } close $in; close $out;

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

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

    Your code throws the following error

    Argument "10 0^I" isn't numeric in numeric eq (==) at ./4_Multiply.pl line 16, <$in> line 1.

    Any idea why this is happening

      the first line of your input file is
      10 0<tab>
      and not just a straight number as you were expecting?

      (Or are we supposed to be dealing with more than one number on a line? You don't really say in your original post.)