リストをランダムに並び替えるPerlスクリプト(続き)
昨日(id:tamago_girai:20070402)の話のつづき。結局こんな感じになった。
#!/usr/bin/env perl -w # shuffle.pl # -*- Mode: Perl; coding: shift_jis -*- # for Perl 5 or later only (maybe). # # ディレクトリ内のファイル名を取ってきて # ランダムに並び替えて表示 # use strict; use warnings 'all'; my @filelist = glob("./*.tex"); # とりあえずtex my @newarray; my $max = @filelist; # $maxは配列の要素数 my %hash; for my $element (@filelist) { my $number = rand($max); # てきとうに数字をとる while (defined $hash{$number}) { $number = rand($max); } $hash{$number} = $element; } for my $hashkey (sort { $a <=> $b } keys %hash) { push(@newarray, $hash{$hashkey}); } for (@newarray) { print $_; print "\n"; } # shuffle.pl ends here.
以下出力結果。
perl shuffle.pl ./No_21.tex ./No_27.tex ./No_00.tex ./No_20.tex ./No_29.tex ./No_15.tex ./No_30.tex ./No_12.tex ./No_06.tex ./No_22.tex ./No_05.tex ./No_16.tex ./No_17.tex ./No_09.tex ./No_08.tex ./No_04.tex ./No_11.tex ./No_18.tex ./No_10.tex ./No_02.tex ./No_26.tex ./No_28.tex ./No_14.tex ./No_25.tex ./No_01.tex ./No_19.tex ./No_13.tex ./No_07.tex ./No_24.tex ./No_03.tex ./No_23.tex