perl操作数组
#!/usr/bin/perl -w#test the string
print "hello" ." ". "world!\n"; #字符串连接
print "hello" x 4 . "\n";
print "ha" x (2+3) . "\n";
print "12ggdd" * 3 ."\n"; #启用告警时,即#!/usr/bin/perl -w时,程序提示告警信息.
#计算从1到100的所有奇数相加.
$num=1;
#$sum=0;
while ($num < 100){
$sum+=$num;
#print $num ."\n";
$num+=2;
}
print $sum;
#test the list
#print "=============test the list=============";
#@listA= (1..100);
#foreach $item (@listA){
# print $item;
#}
#test pop method
print "\n\n";
@arr=5..9;
$fred1=pop(@arr);
$fred2=pop(@arr); #the value of $fred2 is 8.
printf "%s,%s",$fred1,$fred2;
#testpush method
print "\n\n";
foreach $item (@arr){
print $item . "\n";
}
print "\n\n";
push (@arr,10); #push 一个元素10
push @arr,20;#push 一个元素20
push @arr ,1..10; #push 一个元素1..10
foreach $item (@arr){
print $item . "\n";
}
print "\n\n";
@brr=reverse(@arr);#数组元素,翻转.
foreach $item (@brr){
print $item . "\n";
}
print "\n\n";
@crr=sort(@arr);#数组元素,排序.
foreach $item (@crr){
print $item . "\n";
}
print "write some words :\n";
while(chomp($line=<STDIN>))
{
print$line ."\n";
if (lc($line) eq "yes") #lc/uc/lcfirst/ucfirst
{
print "break out !\n";
last;
}
}
页:
[1]