Hello all,
I have aray @arr:
I want the output to be like this:Code:@arr = (14,13,6,17,15,53); If I want to print values that is greater than its previous values, i.e. I want to print: for ($i=0;$i<$array_size;$i++) { If ($i greater than its previous values) {Print $i;} }
14
17
53
Please write the code in place of red line
N.B: I don't want to sort array, I want it in the same order...
Thanks all![]()
Last edited by WingedPanther; 06-22-2009 at 08:53 AM. Reason: add code tags (the # button)
Very simple...
Code:@arr = (14,13,6,17,15,53); for my $i (1 .. $#arr) { if ($arr[$i] > $arr[$i-1]) { print "Whatever you want to print here\n"; } }
Thanks KevinADC for your help...
but i have a point, I want the array to compare an elemet with its all previous elemts, not with the one before it i.e. if the array is as follow:
@arr = (14,13,20,6,17,15,53);
I want it to print:
20
53
NOT 17, because there is an element greater than 17 previous to it which is 20.
I have made the following piece of code:
sub max {
my $max = $_[0];
for ( @_[ 1..$#_ ] ) {
$max = $_ if $_ > $max;
}
$max
}
for ($i=0;$i<=$size;$i++)
{
if ($arr[$i]== max (@arr[0..$i]))
{print $arr[$i];}
}
it gives a new range for searching starting from index 0 & ends with index i.
It works well, but the performance is very low specially with huge no. of elements.
if you can help me to make it searches every iteration not from 0 to i, but from previous i to new i, you be thankful...
Thanks,
See how this works:
Code:my @arr = (14,13,20,6,17,15,53); my @new = reverse @arr; my @results; LOOP1: for my $i (0..$#new) { for my $j ($i+1..$#new) { next LOOP1 if ($new[$j] > $new[$i]); } push @results, $new[$i]; } print "$_\n" for reverse @results;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks