Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

calculate sum of numbers inside a text file

by CR18 (Initiate)
on Sep 27, 2015 at 02:04 UTC ( [id://1143121]=perlquestion: print w/replies, xml ) Need Help??

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

I have a text file that contains lines of numbers. Is there a way to use array to call inside and return all + numbers and - numbers? Thanks for your help. I have a script that read the array I input, but I am wondering if this could be used to read array of numbers inside a text file. Thanks a million. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
use strict; my @array = @ARGV ? @ARGV : qw( -1 1 7 -4 3 -51 4 47 -87); #****************************************** #\sorting the array from small to large # HERE @array = ( @array ); @array = sort { $a <=> $b } @array; #****************************************** #\ my $sumAll=0; for (@array){ #\sum of all numbers HERE sumAll $sumAll +=$_; } my $cur = my $best_start = my $best_end = my $cur_start = 0; my $sumPos = $array[0]; for (0..$#array) { $cur += $array[$_]; if ($sumPos < $cur) { $sumPos = $cur; $best_start = $cur_start; $best_end = $_; } if ($cur < 0) { $cur = 0; $cur_start = $_ + 1; } } my $sumNeg = $sumAll-$sumPos; #\Sum of negative[sumNeg] = sumAll-s +umPos print qq(Start: $best_start End: $best_end Numbers: @array[$best_start..$best_end]\n\n Order of Array: @array Sum of All: $sumAll Sum of positive: Chr1, $sumPos Sum of Negative: Chr1, $sumNeg );
In my text file/data file, I have -1 1 7 -4 3 -51 4 47 -87 (randomly placed) top to bottom. So, it read as a column.

Replies are listed 'Best First'.
Re: calculate sum of numbers inside a text file
by davido (Cardinal) on Sep 27, 2015 at 03:06 UTC

    while (<>) { chomp; next unless length; if ($_ < 0) { push @negatives, $_; } else { push @positives, $_; } }

    Update: This code was posted before the OP added code to his post.


    Dave

      Thanks Dave.
        Can I put this while loop inside my array so that the array will first get the data from the file, and then run the calculation?
Re: calculate sum of numbers inside a text file
by Athanasius (Archbishop) on Sep 27, 2015 at 07:35 UTC

    Hello CR18, and welcome to the Monastery!

    You should look into the core module List::Util, and the module List::MoreUtils. They provide useful functionality for processing arrays:

    #! perl use strict; use warnings; use List::Util 'sum0'; use List::MoreUtils 'part'; my @array = @ARGV ? @ARGV : qw( -1 1 7 -4 3 -51 4 47 -87 ); @array = sort { $a <=> $b } @array; my @part = part { $_ >= 0 } @array; printf "Order of array: %s\n", join ' ', @array; printf "Sum of all: %d\n", sum0 @array; printf "Sum of positive: %d\n", sum0 @{ $part[1] }; printf "Sum of negative: %d\n", sum0 @{ $part[0] };

    Output:

    17:47 >perl 1384_SoPW.pl Order of array: -87 -51 -4 -1 1 3 4 7 47 Sum of all: -81 Sum of positive: 62 Sum of negative: -143 17:47 >

    Update: Simplified code by replacing sum with sum0.

    Some notes on your code:

    • It’s good to see that you use strict, but you should also use warnings.

    • This line: @array = ( @array ); does nothing.

    • This loop:

      for (@array){ #\sum of all numbers HERE sumAll $sumAll +=$_; }

      can be written more succinctly in one line:

      $sumAll += $_ for @array;

      — the expressive power of Perl!

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: calculate sum of numbers inside a text file
by 1nickt (Canon) on Sep 27, 2015 at 02:14 UTC

    No.

    An array can't call anything. It's a data type.

    But there is a way to use Perl to read the text file and return the positive and negative numbers in separate arrays. In fact, there's more than one way to do it.

    You should show a short example program demonstrating what you have so far, so that people can help you. Also post a sample of your data file and an example of what you want the output to be. Please read up on how best to post a question in the PerlMonks FAQ linked to the right. If you haven't got as far as a working or failing test program, familiarize yourself with the basics of perl with perlintro.

    The way forward always starts with a minimal test.
      Thanks. I just put the same set of numbers inside a text file and see if that would work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found