Thread Sort() (10 answers)
Opened by taikahn22a at 2008-05-08 22:02

FIFO
 2008-05-08 22:20
#109464 #109464
User since
2005-06-01
469 Artikel
BenutzerIn

user image
Der Teilungsrest ist der Modulus, Operator ist % vgl. perlop, es müsste heißen:

Code (perl): (dl )
1
2
3
4
5
6
use strict;
use warnings;

my @array = (1..20);
my @array3 = sort { $a % 5 <=> $b % 5 } @array;
print @array3;


$a und $b müssen evtl. die Plätze tauschen, je nachdem, ob Du auf- oder absteigend sortieren willst. In Deinem Beispiel taucht jeder Rest ohnehin mehrmals auf, so dass die Sortierung auch durch die Reihenfolge im Ausgangs-Array beeinflusst wird (mehrere Elemente mit gleichem Rest).

[edit: perl-tags]
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"

View full thread Sort()