Re: More loops



From: "Amichai Teumim" <amichai@xxxxxxxxxx>
I need to use two loops and an if statement to sort the contents of
this array so
that the number go from lowest to highest.

@sorted = sort {$a <=> $b} @unsorted;

You can use the builtin function sort(). All you have to do is to
tell it how do you want the elements compared.

So I tried to implement this:

#!/usr/bin/perl

@array = (5,3,2,1,4);

for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
if ($array[j+1] < $array[j]) { /* compare the two neighbors */
tmp = $array[j]; /* swap $array[j] and $array[j+1] */
$array[j] = $array[j+1];
$array[j+1] = tmp;
}
}

The first thing you are missing are sigils. All variables in Perl
have to start with a sigil ($, @, %, ...).

for ($i=0; $i<$n-1; $i++) {
for ($j=0; $j<$n-1-$i; $j++)
if ($array[$j+1] < $array[$j]) { /* compare the two neighbors
*/
$tmp = $array[$j]; /* swap $array[j] and $array[j+1]
*/
$array[$j] = $array[$j+1];
$array[$j+1] = $tmp;
}
}

Next thing is that you do not need a third variable to exchange the
values of two variables.

($a, $b) = ($b, $a);

is perfectly legal and more efficient.
So you can write the body of the inner loop like this:

($array[j], $array[j+1]) = ($array[j+1], $array[j]);

You may even use so called "array slices". That is instead of writing
($array[j+1], $array[j])
you can write just
@array[j+1,j]
so the body of the loop will be

@array[j,j+1] = @array[j+1,j];

You may also use the foreach style of loop instead of the C-style
for():

foreach my $i (0 .. $#array) {
foreach my $j (0 .. $#array-1-$i) {
if ($array[$j+1] < $array[$j]) {
@array[j,j+1] = @array[j+1,j];
}
}
}


foreach $elem (@array){
print "$elem\n";
}

This can be simplified to

print join("\n", @array), "\n";

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: fastest way to sort 2-D variant arrays?
    ... and then change the FSO loop section to loop through the ... array, and add the elements to the recordset using the AddNew statement. ... How would I write my variant array to the recordset? ... Here is something that I use on an ASP page to sort files by date there ...
    (microsoft.public.vb.general.discussion)
  • Re: fastest way to sort 2-D variant arrays?
    ... and then change the FSO loop section to loop through the ... array, and add the elements to the recordset using the AddNew statement. ... How would I write my variant array to the recordset? ... Here is something that I use on an ASP page to sort files by date there ...
    (microsoft.public.vb.general.discussion)
  • Re: fastest way to sort 2-D variant arrays?
    ... The actual sort of the recordset is very fast, but what makes it slow is ... I take there is no other way to this than in a loop with: ... array, and add the elements to the recordset using the AddNew statement. ...
    (microsoft.public.vb.general.discussion)
  • Re: confused with sort and foreach ($EmailList as $key => $value) {..}
    ... I am trying to sort a multi-dimensional array and the loop through the ...
    (comp.lang.php)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)