For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > More loops









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author More loops
Amichai Teumim

2007-06-28, 6:59 pm

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.

#!/usr/bin/perl

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

## include your code here ##

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

Looking further into this it was revealed to me that I should use a
bubble loop. Such as this:

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

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

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

I know I'm close (right?). What am I missing here?

Thanks

Amichai

Jenda Krynicky

2007-06-28, 6:59 pm

From: "Amichai Teumim" <amichai@teumim.com>
> 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@Krynicky.cz === 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

Chas Owens

2007-06-28, 6:59 pm

On 6/28/07, Jenda Krynicky <Jenda@krynicky.cz> wrote:
> From: "Amichai Teumim" <amichai@teumim.com>

snip
>
> This can be simplified to
>
> print join("\n", @array), "\n";

snip

or (since this is Perl and TIMTOWTDI)

print map { "$_\n" } @array;
Abpoted

2007-06-30, 7:43 am

Ashlee Simpson , Panties sliding down a perfectly round ass!

http://www.yourtubeaudio.com/e?vid=1673286
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com