perlで配列の配列。勤務する人々を1...と入力ファイルを作って出力させると、週休が出るプログラム。



#!/usr/bin/perl
#
#
#---------infile (1 2 3 4 .... 11 12 13 14 15 16)------------

$| = 1;

if ($#ARGV < 0) {
print STDERR "\n";
print STDERR "Usage: weekly_holiday.pl infile.txt";
print STDERR "\n";
exit;
} else {
$infile = $ARGV[0];
}

open(INFILE, "<$infile") || die "Can't open $infile";
while (<INFILE>) {
chomp;
push(@all, split(/\t/, $_));
}

#print "@all\n";
#exit;

@week = ("m","t","w","th","f","s");

@randall = ();
@randweek = ();

while (@all) {
push(@randall, splice(@all, rand@all , 1));
}
#print "@randall\n";

while (@week) {
push(@randweek, splice(@week, rand@week , 1));
}
#print "@randweek\n";


@m = ("m");
@t = ("t");
@w = ("w");
@th = ("th");
@f = ("f");
@s = ("s");

@weekarray = ([@m], [@t], [@w], [@th], [@f], [@s]);

foreach $h (0..$#randall) {
$hash{$randall[$h]} = $randweek[$h%6];

$temp_nurse = $randall[$h];
$tempweek = $randweek[$h%6];
foreach $i (0..$#weekarray) {
if ($i == 0) {
push(@m, $temp_nurse) if ($weekarray[0][0] eq $tempweek);
} elsif ($i ==1) {
push(@t, $temp_nurse) if ($weekarray[$i][0] eq $tempweek);
} elsif ($i ==2) {
push(@w, $temp_nurse) if ($weekarray[$i][0] eq $tempweek);
} elsif ($i ==3) {
push(@th, $temp_nurse) if ($weekarray[$i][0] eq $tempweek);
} elsif ($i ==4) {
push(@f, $temp_nurse) if ($weekarray[$i][0] eq $tempweek);
} elsif ($i ==5) {
push(@s, $temp_nurse) if ($weekarray[$i][0] eq $tempweek);
}
}
# print "key= $randall[$h] val = $randweek[$h%6]\n";

}
print "@m\n@t\n@w\n@th\n@f\n@s\n";

出力は、
[minako1@minako ~/kinmu]# perl weekly_holiday.pl infile.demo
m 13 2
t 4 9 16
w 11 8 10
th 7 3 14
f 6 1 15
s 5 12

いつもと違うのはperlで配列の配列というのをつくったこと。
でも不細工な形。

perlで2次元配列、というのか??

http://www.studiohs.com/28if/develop/tips/Perl/index.html
とかいろいろあり。