perl数组以及qw
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 apple3.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."\n";
print $trees."\n";
$trees = 'pine';
print "@trees";
输出:
oak
apple
oak cedar maple apple pine
@trees = qw(oak cedar maple apple cherry pine peach fir);
@conifers = @trees;
print @trees."\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)%7Bbr/>}@numbers;
print Dumper(@sorted);
输出:
$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;
$hangman = "0-|--
页:
[1]