perl数组以及qw
admin
2023-07-28 10:21:09
0

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(){
($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 = ; 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。

相关内容

热门资讯

浙江宣传:“走个面儿”咋就没面... “咱北京两千多万人口,您受累,您走个面儿,把这第一波的票房带起来,咱就有了。”某知名导演的新片首映礼...
辞职声明仅95秒遭质疑,韩国队... 【环球时报综合报道】美加墨世界杯小组赛出局后,韩国队主教练洪明甫当地时间28日在墨西哥的韩国队大本营...
美媒爆料:美军第五舰队总部遭伊... 据美国《华尔街日报》27日报道,其通过对卫星图像、社交媒体视频和五角大楼记录的分析发现,今年2月底至...
英国智库给菲律宾GDP增速“浇... 【环球时报特约记者 叶满】英国经济研究机构凯投宏观发布的最新一期《亚洲经济展望》报告(以下简称“报告...
欧洲持续高温,有华人用冰箱降温... 连日来,欧洲多国迎来罕见极端高温天气,法国、德国、意大利等地气温持续飙升,部分地区突破40摄氏度。受...
伊副外长强调船只须按“伊朗线路... 伊朗外交部副部长加里巴巴迪当地时间29日晚间在接受采访时强调,所有船只均须按照“伊朗线路”通过霍尔木...
委内瑞拉强震已致1719人死亡 当地时间29日,委内瑞拉全国代表大会主席罗德里格斯通报,地震已造成该国1719人死亡,5034人受伤...
铋晟新材料申请氯氧化铋基复合材... 国家知识产权局信息显示,江苏铋晟新材料有限公司申请一项名为“一种氯氧化铋基复合材料及其制备方法和用途...
韩国政府将投资千万亿韩元于AI... 韩国总统李在明29日在总统府青瓦台主持召开会议,公布总额超千万亿韩元的半导体、物理人工智能(AI)和...
以色列防长称以伊可能随时再起冲... △卡茨(资料图)据以色列方面29日消息,以国防部长卡茨当天表示,鉴于复杂的安全局势和在黎巴嫩的军事行...