2007/10/12, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:

>
> Here is what I had so far. I'm not sure exactly what I'm doing wrong.
>
> !/usr/bin/perl
>
> @array = (5,3,2,1,4);
> @tmp_array;
>
> $i = 0;
> while($i <= 5){
>    if ($array[$i] > $array[$i+1]);
> }
>    while ($i > 2) {
>    print "$tmp_array\n";
>    $i--;
> }
>
> foreach $elem (@array){
>   print "$elem";
> }
>

Wow,too many wrong syntax and logic in the code above.Always to add
'use strict' and 'use warnings' at the begin of your code,it would
avoid most syntax problems.

I learned the popup sort method in my university time (with C language
class at that time).
I just wrote one with Perl again,maybe it's not so exactly,but just do
what you wanted.


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


for (0 .. $#array -1) {
  for (0 .. $#array -1) {
    if ($array[$_] > $array[$_+1]) {
        my $tmp = $array[$_];
        $array[$_] = $array[$_+1];
        $array[$_+1] = $tmp;
    }
  }
}
print "@array";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to