---
title: "perl操作数组"
author: "luogen33"
date: "2011-09-06T23:34:14Z"
tags: []
categories: ["编程语言"]
slug: "perl"
draft: false
lastmod: "2011-09-06T23:34:14Z"
id: 1167157
url: "https://luogen33.iteye.com/blog/1167157"
---

#!/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; 
#test  push 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; 
   } 
}