Perl Learning - 5 (sort(), reverse(), context, list <
发布时间:2020-12-15 21:02:52 所属栏目:大数据 来源:网络整理
导读:reverse() sort() ? reverse() returns reversed list. ? @fred=6 .. 10; @barney=reverse(@fred);??# got (10,9,8,7,6) @wilma=reverse 6 .. 10;??# same as above @fred=reverse @fred;??# reverse itself reverse @fred;???# Doesn't work! ? Notice that
reverse() & sort()
?
reverse() returns reversed list.
?
@fred=6 .. 10;
@barney=reverse(@fred);??# got (10,9,8,7,6) @wilma=reverse 6 .. 10;??# same as above @fred=reverse @fred;??# reverse itself
reverse @fred;???# Doesn't work!
?
Notice that reverse doesn't change the original array,we must give the reversed array back to itself to change.
?
sort() returns sorted list by order.
It's sorted default by ASCII order: Upper char is before lower char,number is before char.
Wild chars are among numbers and nomal characters.
?
@rocks=qw/bedrock slate rubble granite/;
@sorted=sort(@rocks); @back=reverse sort @rocks; @rocks=sort @rocks;??# sort itself @numbers=sort 97 .. 102;?# got 100,101,102,97,98,99
?
By default sort won't sort() numbers by math order,it always by ASCII order by default.
Like reverse,sort() doesn't change the original array itself.
?
** Scalar or List CONTEXT **
The author of <<Learning Perl>> said *contxt* is the most important part of this chapter,maybe even the most important of this book.
?
A?given expression has different meanings in different contexts,just like human languages.
When Perl tries to expain expressions,it expects a scalar or a list,that is the context of exression.
?
42+something?# something must be scalar
sort something?# something must be list
@people=qw(fred barney ketty);
@sorted=sort @people;??# list context $number=42+@people ;??# scalar context
@list=@people;??# list context: list of 3 people
@n=@people;??# scalar context: number 3
?
sort() a scalar gets a undef,reverse() a scalar gets reversed characters.
?
@backwards=reverse qw/yabaa dabba doo/;?# get "doo dabba yabba"
$backwards=reverse qw/yabba dabba doo/; # get "oodabbadabbay"
?
Some more common scalar context for 'something':
?
$fred=something;
$fred[3]=something; 123+something; something+654; if(something){...} $fred[something]=something;
?
Some more common list context for 'something':
?
@fred=something;
($fred,$barney)=something; ($fred)=something; push @fred,something; foreach $fred(something) sort something; reverse something; print something;
?
scalar() can produce a scalar context by force,it just tells Perl it provides a scalar context,nothing else.
?
@rocks=qw(talc quartz jade obsidian);
print "How many rocks do you have?n"; print "I have ",@rocks,"rocks!n";??# wrong! print() list context,will print rocks' names print "I have ",scalar @rocks,"rocks!n";?# right,prints the number
?
In scalar context,<STDIN> returns one line from input;
In list context,<STDIN> returns all lines from input,each line is one element of array.
If input comes from a file,context <STDIN> gets all lines of the file;
If input comes from keyboard,context <STDIN> gets all lines user entered,end by CTRL+D,in Unix style systems,Windows CTRL+Z.
Each element includes line contents with n in the end. chomp() can remove all n for every element.
?
@lines=<STDIN>;
chomp (@lines);
?
Or more popular style:
?
chomp(@lines=<STDIN>);?# read all lines,exclude n
?
Exercises:
?
1. Write a program,read some characters (different lines) into a list,print the reversed lines.
?
#!/usr/bin/perl
my @lines;
chomp(@lines=<STDIN>);
@lines=reverse @lines;
foreach(@lines){ ??? print "$_n"; ??? } ##############################
?
2. Write a program,read some numbers (one number each line),print all the crosponding names (listed below). Put these names in your program.
fred betty barney dino Wilma pebbles bamm-bamm
?
#!/usr/bin/perl
my @names;
my @numbers;
@names=qw(fred betty barney dino Wilma pebbles bamm-bamm);
chomp(@numbers=<STDIN>); foreach(@numbers){ ??? print "$names[$_-1]n"; ??? } ##############################
?
3. Write a program,read some characters (in different lines) into a list. Sort them by ASCII,print in one/multiple lines.
?
#!/usr/bin/perl
my @lines;
chomp(@lines=<STDIN>); @lines=sort @lines;
print "One line style: @linesn";
print "Multiple lines style:n";foreach(@lines){??? print "$_n";??? }##############################
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |