perl允許你用任何標點符號作為定界符。常用的寫法有:
#!usr/bin/perl -w
use strict;
=pod
my @arr = ("fred", "barney", "betty", "wilma", "dino");
my @arr = qw(fred barney betty wilma dino);#同上,但更簡潔,也更少鍵入
my @arr = qw! fred barney betty wilma dino!;
my @arr = qw/ fred barney betty wilma dino/;
my @arr = qw# fred barney betty wilma dino#;
my @arr = qw{ fred barney betty wilma dino};
my @arr = qw[ fred barney betty wilma dino];
=cut
my @arr = qw< fred barney betty wilma dino>;
for(my $i = 0; $i < @arr; $i++){
print $arr[$i]."\n";
}
輸出:
fred
barney
betty
wilma
dino
互換兩者的值
#!usr/bin/perl -w
use strict;
my $a = "apple";
my $b = "banana";
=pod
my $c = $a;
$a = $b;
$b = $c;
=cut;
($a, $b) = ($b, $a);
print "變量a: ".$a."\n變量b: ".$b;
輸出:
變量a: banana
變量b: apple
4.1 將數據放入列表和數組
@array = (5,'apple',$x,3.1415926);
print "@array";#數組中的內容
$count = @array;
print "\n".$count;#數組的個數
print "\n".$#array;#數組的最大索引
輸出:
5 apple 3.1415926
4
3
@array = qw(5 apple $x 3.1415926);
print "@array";#數組中的內容
$count = @array;
print "\n".$count;#數組的個數
print "\n".$#array;#數組的最大索引
輸出:
5 apple $x 301415926
4
3
@array = (1..10);
print "@array";#數組中的內容
$count = @array;
print "\n".$count;#數組的個數
print "\n".$#array;#數組的最大索引
輸出:
1 2 3 4 5 6 7 8 9 10
10
9
@array = (1..10,20..30);
print "@array";#數組中的內容
$count = @array;
print "\n".$count;#數組的個數
print "\n".$#array;#數組的最大索引
輸出:
1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30
21
20
@cope = @origina ;
$clean = ();
@girls = qw(Merciz Jan Cindy);
@boys = qw(Greg peter Bobby);
@kids = (@girls, @boys);
@family = (@kids,('Mile','Carol'), 'Alice');
print "@family\n";
$count = @family;
print $count."\n";
print $#family;
輸出:
Merciz Jan Cindy Greg peter Bobby Mile Carol Alice
9
8
@family = qw(Merciz Jan Cindy Greg peter Bobby Mile Carol Alice);
print "@family\n";
$count = @family;
print $count."\n";
print $#family;
輸出:
Merciz Jan Cindy Greg peter Bobby Mile Carol Alice
9
8
($a , $b ,$c) = qw(apples oranges bananas);
print $a."\n";
print $b."\n";
print $c."\n";
輸出:
apples
oranges
bananas
($a,@fruit,$c) = qw(peaches mangoes grapes cherries);
print $a."\n";
print "@fruit\n";
print $c;
輸出:
peaches
mangoes grapes cherries
($t,$u,$v) = qw(partridge robin cardinal quail);
print $t."\n";
print $u."\n";
print $v."\n";
輸出:
partridge
robin
cardinal
($a,$b,$c,$d) = qw(squirrel woodchuck gopher);
print $a."\n";
print $b."\n";
print $c."\n";
print $d;
輸出:
squirrel
woodchuck
gopher
4.2 從數組中取出元素
@trees = qw(oak cedar maple apple);
print $trees[0]."\n";
print $trees[3]."\n";
$trees[4] = 'pine';
print "@trees";
輸出:
oak
apple
oak cedar maple apple pine
@trees = qw(oak cedar maple apple cherry pine peach fir);
@conifers = @trees[5,6];
print @trees[5,6]."\n";
print @conifers;
輸出:
peach
pinepeach
4.2.1 尋找結尾
@trees = qw(oak cedar maple apple cherry pine peach fir);
print $#trees;
輸出:
7
4.2.2 關于上下文的詳細說明
@foo = qw( water pepsi coke lemonade );
$a = @foo;
$b = $#foo;
print "$a\n";
print "$b";
輸出:
4
3
@mydata = qw( oats peas beans barley );if(@mydata){
br/>if(@mydata){
}
輸出:
The array has elements!
4.2.3 回顧以前的幾個功能
@mydata = qw( oats peas beans barley );
print @mydata."\n";
print scalar (@mydata);
輸出:
4
4
$last_pet = ('cat','dog','fish','canary','iguana');
print $last_pet;
輸出:
iguana
use Data::Dumper;
print Dumper(localtime);
輸出:
$var1 = 2;
$var2 = 59;
$var3 = 15;
$var4 = 17;
$var5 = 11;
$var6 = '110';
$var7 = 5;
$var8 = 350;
$var9 = 0;
($sec, $min, $hour, $mday, $mon, $year_off, $wday, $yday, $isdst) = localtime;
print $sec."\n";
print $min."\n";
print $hour."\n";
print $mday."\n";
print $mon."\n";
print $year_off."\n";
print $wday."\n";
print $yday."\n";
print isdst;
輸出:
58
3
16
17
11
110
5
350
4.3.1 遍歷數組
@flavors = qw( chocloate vanilla strawberry mint sherbert );
for($index=0; $index<@flavors; $index++){
print "My favorite flavor is $flavors[$index] and \n";
}
print "many others.\n";
輸出:
My favorite flavor is chocloate and
My favorite flavor is vanilla and
My favorite flavor is strawberry and
My favorite flavor is mint and
My favorite flavor is sherbert and
many others.
@flavors = qw( chocloate vanilla strawberry mint sherbert );
foreach $cone (@flavors){
print "I'd like a cone of $cone\n";
}
輸出:
I'd like a cone of chocloate
I'd like a cone of vanilla
I'd like a cone of strawberry
I'd like a cone of mint
I'd like a cone of sherbert
4.3.2 在數組與標量之間進行轉換
@words = split(/ /,"The quick brown fox");
print @words."\n";
print "@words";
輸出:
4
The quick brown fox
while(<STDIN>){
($firstchar) = split(//,$_);
print "The first character was $firstchar\n";
}
輸出:
456
The first character was 4
@Music = ('White Album,Beatles',
'Graceland,Paul Simon',
'A Boy Named Sue,Goo Goo Dolls');
foreach $record (@Music){
($record_name,$artist) = split(/,/,$record);
print $record_name."\n";
print $artist."\n";
}
輸出:
White Album
Beatles
Graceland
Paul Simon
A Boy Named Sue
Goo Goo Dolls
$numbers =join(',',(1..10));
print $numbers;
輸出:
1,2,3,4,5,6,7,8,9,10
$message = "Elvis was here";
print "The string \"message\" consists of:".join('-',split(//,$message));
輸出:
The string Elvis was here consists of:E-l-v-i-s- -w-a-s- -h-e-r-e
4.3.3 給數組重新排序
@Chiefs = qw( Clinton Bush Reagan Carter Ford Nixon );
print join(' ',sort @Chiefs);
輸出:
Bush Carter Clinton Ford Nixon Reagan
use Data::Dumper;
@numbers = qw(7 8 2 4 5);
@sorted = sort {return (1) if($a > $b);
return (0) if($a == $b);
return (-1) if($a < $b);}@numbers;
br/>}@numbers;
輸出:
$VAR1 = 2;
$VAR2 = 4;
$VAR3 = 5;
$VAR4 = 7;
$VAR5 = 8;
use Data::Dumper;
@numbers = qw(7 8 2 4 5);
@sorted = sort { $a<=>$b }@numbers;
print Dumper(@sorted);
輸出:
$VAR1 = '2';
$VAR2 = '4';
$VAR3 = '5';
$VAR4 = '7';
$VAR5 = '8';
@lines = qw(I do not like green eggs and ham);
print join(' ',reverse @lines);
輸出:
ham and eggs green like not do I
@lines = qw(I do not like green eggs and ham);
print join(' ',reverse sort @lines);
輸出:
not like ham green eggs do and I
4.4練習:做一個小游戲
#!/usr/bin/perl -w
@words = qw( internet answers printer program);
@guesses = ();
$wrong = 0;
$choice = $words[rand @words];
$hangman = "0-|--<";
@letters = split(//,$choice);
@hangman = split(//,$hangman);
@blankword = (0) x scalar(@hangman);
OUTER:
while($wrong < @hangman){
foreach $i(0..$#letters){
if($blankword[$i]){
print $blankword[$i];
}else{
print "-";
}
}
print "\n";
if($wrong){
print @hangman[0..$wrong-1];
}
print "\n Your Guess: ";
$guess = <STDIN>; chomp $guess;foreach(@guesses){
br/>foreach(@guesses){
}
$right = 0;
for($i=0; $i < @letters; $i++){
if($letters[$i] eq $guess){
$blankword[$i] = $guess;
$right = 1;
}
}
$wrong++ unless(not $right);
if(join('',@blankword) eq $choice){
print "You got it right!\n";
exit;
}
}
print "$hangman\nSorry,the word was $choice.\n";
4.6.2 思考題
1) 如果要將$ a和$ b這兩個標量變量中包含的值進行交換,哪個方法最有效?
a. $a=$b;
b. ($a,$b)=($b,$a);
c. $c=$a;$a = $b;$b = $c;
2) 語句$a = scalar(@array);將什么賦值給變量$a?
a. @array中的元素的數量;
b. @array的最后一個元素的索引;
c. 該語句無效。
4.6.3 解答
1) 答案是b。
2) 答案是a。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。