温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

perl数组以及qw

发布时间:2020-07-07 07:47:48 来源:网络 阅读:1328 作者:nxf198609 栏目:开发技术

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。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI