By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,844 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,844 IT Pros & Developers. It's quick & easy.

Newbie trying to understand nested loops

P: 2
I am trying to understand the following code:

Expand|Select|Wrap|Line Numbers
  1.  my @arr1=(5,3,2,1,4);
  2.  my $i=0;
  3.  my $j=0;
  4.  $tmpnum
  5.  for($i=0;$i<@arr1;$i++) {
  6.   for($j=0;$j<@arr1;$j++)   {
  7.    if($arr1[$i]<$arr1[$j])       {
  8.      $tmpnum=$arr1[$i];
  9.     $arr1[$i]=$arr1[$j]
  10.   $arr1[$j]=$tmpnum;
  11.   };
  12.  }; 
  13. };
  14.  
  15.  
  16.  foreach(@arr1) {
  17.   print $_ . "\n";
  18. }
When I look at the first for loop I know that @arr is 5 (which represents the total number of indexes). So then the condition is met because 0 is less than 5. I am then assuming that the Perl moves onto the second loop and the condition is met also and Perl then moves onto the if statement. Now here is where I get confused. If this were the first run of the script the condition in the if statement would not be meet because 5 is not less than 5 ($i and $j would both be 5 because they would be both zero and zero represents the first index which is 5. So if that is the case does the script exit the second loop and go back to the first one and increment by 1 or does it go back to the second loop and increment by 1? I believe I was told that it goes to the second loop so $j which was 0 would increment to 1. If that is the case the if statement would the read as if($arr1[5]<$arr1[3] (5 and 3 representing the index based on the variable) and that does not make any sense to me especially since J would keep on incrementing and $j would never be greater than $i. If I go back to the first loop that does not make any sense to me either because $i would increment by 1 and then I would have to move onto the next loop and $j would increment by 1 and when I get to the if condition I am stuck because 3 would not be less than 3 (3 representing the second index of the array). What am I missing here?
Apr 12 '08 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Your interpretation of the first run through the first loop is correct, but only because your first element is the largest. It might help if you have some proper indenting of your code:

Expand|Select|Wrap|Line Numbers
  1. my @arr1=(5,3,2,1,4);
  2. my $i=0;
  3. my $j=0;
  4. $tmpnum # should be my $tmpnum;
  5. for($i=0;$i<@arr1;$i++) {
  6.     for($j=0;$j<@arr1;$j++) {
  7.         if($arr1[$i]<$arr1[$j]) {
  8.             $tmpnum=$arr1[$i];
  9.             $arr1[$i]=$arr1[$j]
  10.             $arr1[$j]=$tmpnum;
  11.         };
  12.     };
  13. };
  14.  
  15. foreach(@arr1) {
  16.     print $_ . "\n";
  17. }
Ahh, much better and easier to read! Now, about those loops:

We enter loop 1: $i is 0.
We enter loop 2: $j is 0.
Is $arr1[$i] < $arr1[$j]? Well, $i = 0, $j = 0, $arr1[0] = 5, and 5 is not less than 5. So the if statement is not executed. We skip past the if statement.
Exit loop 2. We increment $j to 1.
Is $j < @arr1? $j = 1, @arr1 = 5, 1 < 5, so the loop goes again. Enter loop 2.
Is $arr1[$i] < $arr1[$j]? Well, $i = 0, $j = 1, $arr1[0] = 5, $arr[1] = 3, and 5 is not less than 5. So the if statement is not executed. We skip past the if statement.

(blah blah blah, until...)
We increment $j to 5.
Is $j < @arr1? $j = 5, @arr1 = 5, and 5 is not less than 5, so we no longer execute loop 2.
Exit loop 1. Increment $i to 1. Check: is $1 < @arr1? Yes, because 1 < 5. Enter loop 1.
Enter loop 2: $j = 0 again! Not 1!
Is $arr1[$i] < $arr1[$j]? Well, $i = 1, $j = 0, $arr1[1] = 3, $arr1[0] = 5, and 3 < 5! So the if statement executes!
The if statement swaps the two values, making @arr1 contain (3,5,2,1,4).
Exit loop 2. We increment $j to 1.

(blah blah blah)

Hopefully that explains the nested loops in this circumstance a little better.
Apr 12 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
The code has a couple of syntax errors but assuming those are fixed, the code is doing a basic bubble sort. Does the same as:

Expand|Select|Wrap|Line Numbers
  1. @arr1 = sort {$a <=> $b} @arr1;
Apr 12 '08 #3

eWish
Expert 100+
P: 971
Kevin's example it much easier to understand. That is the way I would go.

--Kevin
Apr 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.