I'm currently dealing with a sample program I try to understand. Here it is:

---

#!/usr/bin/perl

use strict; use warnings; use diagnostics;

# Array to sort
my @unsortiert = qw(Z A z a 19 91);

# Print array to sort
print "@unsortiert"; print "\n";

# Print positions of unsorted array (0 to last entry #)
my @pos_unsortiert = (0..$#unsortiert);
print "@pos_unsortiert"; print "\n";

# Start sorting
my @pos_sortiert = sort({"$unsortiert[$a]" cmp "$unsortiert[$b]"} 
(0..$#unsortiert));

print "@pos_sortiert"; print "\n";

my @sortiert;
for (0..$#unsortiert)
    {
        $sortiert[$_] = $unsortiert[$pos_sortiert[$_]];
        print "\n$unsortiert[$_] was element $_\t\tand is now element 
$pos_sortiert[$_].";
        };
print "\n\nThis is the sorted result: @sortiert"; print "\n";

---

For unknown reason sorting fails when running the program using an array that 
contains numbers. I'm aware that I try here to compare numbers with the cmp 
operator, but although warnings have be enabled, Perl doesn't get angry about.

So when you run this script as above, you'll notice that the sorting is done 
correctly, but the positions aren't. More strangly when sorting an array of 
strings only, anything is correct (soring and positions output).

Maybe some of you can give me a hint, since I want to understand why it doesn't 
work when using numbers ?

Tobias.


      __________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com

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


Reply via email to