繰り返し
for(一定回数の繰り返し)
# 繰り返し回数
my $count = $ARGV[0];
# 一定回数の繰り返し
if($count > 0){
for($i = 0;$i < $count; $i++){
print "test\n";
}
}else{
print "処理なし";
}
>perl sample.pl 5
test
test
test
test
test
>
foreach(リストの繰り返し)
# リスト設定
@list = ("aaa", "bbb", "ccc", "ddd");
# リストをすべて出力
foreach $var(@list){
print $var . "\n";
}
>perl sample.pl
aaa
bbb
ccc
ddd
>
while(リストの繰り返し)
# リストの設定
@list = ("aaa", "bbb", "ccc", "ddd");
# リスト出力
while(($key, $value) = each @list){
print $key . ":" . $value . "\n";
}
>perl sample.pl
0:aaa
1:bbb
2:ccc
3:ddd
>
while(条件が真の間、繰り返し)
# 設定
my $count = $ARGV[0];
# 回数
if($count > 0){
my $i = 0;
while($i < $count){
print "text\n";
$i++;
}
}else{
print "処理なし";
}
>perl sample.pl 5
text
text
text
text
text
>
do~while(条件が真の間、繰り返し。最低一回は処理を行う)
# 設定
my $count = $ARGV[0];
# 回数
my $i = 0;
do{
print "text\n";
$i++;
}while($i < $count);
>perl sample.pl 5
text
text
text
text
text
>perl sample.pl 0
text
>
until(条件が偽の間、繰り返し)
# 設定
my $count = $ARGV[0];
# 回数
if($count > 0){
my $i = 0;
until($i >= $count){
print "text\n";
$i++;
}
}else{
print "処理なし";
}
>perl sample.pl 5
text
text
text
text
text
>
do~until(条件が偽の間、繰り返し)
# 設定
my $count = $ARGV[0];
# 回数
my $i = 0;
do{
print "text\n";
$i++;
}until($i >= $count);
>perl sample.pl 5
text
text
text
text
text
>perl sample.pl 0
text
>
最終更新:2012年01月03日 00:17