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.