www.farid-hajji.net banner

Farid Hajji

Perl: Einführung, Anwendungen, Referenz (2/e) [Support-Site]

Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2

Beispielprogramm

shuffle-viahash.pl
#!/usr/local/bin/perl -w
# shuffle-viahash.pl -- Ordnet ein Array zufaellig mit Hashes

@input = 'a' .. 'z';                # Umzuordnende Eingabeliste

# Alle Elemente von @input in einem Hash speichern.
@myhash{@input} = ("") x @input;   # Hash-Slices

# Shuffle-Algorithmus:
while ((@remains) = keys %myhash) {
    $myrand = $remains[rand @remains];
    push(@output, $myrand);
    delete $myhash{$myrand};
}

# @output enthaelt die Elemente von @input in zufaelliger
# Reihenfolge. Diese geben wir hier aus:
print "input : ", join("", @input),  "\n";
print "output: ", join("", @output), "\n";
   

[Prev] [Up] [Relevant Chapter] [Next]

[Alte Quelle]


Last modified: $Date: 2006/05/18 12:56:05 $
FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy
FreeBSD Logo