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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.