Closed Thread
Results 1 to 4 of 4

Thread: comparing array values

  1. #1
    mhadidi2002 is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    comparing array values

    Hello all,

    I have aray @arr:

    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;}
    }
    I want the output to be like this:

    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)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: comparing array values

    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";
       }   
    }

  4. #3
    mhadidi2002 is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Re: comparing array values

    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,

  5. #4
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: comparing array values

    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;

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Comparing strings to an array?
    By souldout in forum C and C++
    Replies: 1
    Last Post: 04-13-2011, 09:16 PM
  2. add 2D array values
    By ggmo in forum C and C++
    Replies: 7
    Last Post: 11-14-2010, 05:07 AM
  3. removing index of array of no values
    By mhadidi2002 in forum Perl
    Replies: 3
    Last Post: 06-17-2009, 02:45 PM
  4. Comparing Values
    By John in forum PHP Development
    Replies: 4
    Last Post: 07-25-2006, 04:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts