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。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...