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
@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.
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
@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.
Like reverse, sort() doesn't change the original array itself.
** Scalar or List CONTEXT **
The author of <> 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.
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
sort something # something must be list
@people=qw(fred barney ketty);
@sorted=sort @people; # list context
$number=42+@people ; # scalar context
@sorted=sort @people; # list context
$number=42+@people ; # scalar context
@list=@people; # list context: list of 3 people
@n=@people; # scalar context: number 3
@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"
$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;
$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;
($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
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, returns one line from input;
In list context, returns all lines from input, each line is one element of array.
In list context,
If input comes from a file, context gets all lines of the file;
If input comes from keyboard, context
Each element includes line contents with \n in the end. chomp() can remove all \n for every element.
@lines=;
chomp (@lines);
chomp (@lines);
Or more popular style:
chomp(@lines=); # 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=);
@lines=reverse @lines;
foreach(@lines){
print "$_\n";
}
##############################
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;
my @numbers;
@names=qw(fred betty barney dino Wilma pebbles bamm-bamm);
chomp(@numbers=);
foreach(@numbers){
print "$names[$_-1]\n";
}
##############################
chomp(@numbers=
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=);
@lines=sort @lines;
chomp(@lines=
@lines=sort @lines;
print "One line style: @lines\n";
print "Multiple lines style:\n";
foreach(@lines){
print "$_\n";
}
##############################
foreach(@lines){
print "$_\n";
}
##############################