One previously seen operator that returns a different value in an array context is the
line-input operator,
. As described earlier, returns the next line of input
in a scalar context. Now, in list context, this operator returns all of the remaining lines
up to the end of file. Each line is returned as a separate element of the list. For example:
@lines = ; # read standard input in list context.
hen the input is coming from a file, this will read the rest of the file. But how can
there be an end-of-file when the input comes from the keyboard? On Unix and similar
systems, including Linux and Mac OS X, you’ll normally type a Control-D† to indicate
to the system that there’s no more input;
- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
print "input the lines and end with Ctrl+D\n";
-
-
#open(FILE, 'x_operator.pl') or die "can not open it.";
-
-
my @lines = <STDIN>;
-
#my @lines = <FILE>;
-
#chomp @lines;
-
-
my @newlines = reverse @lines;
-
-
print @newlines;
- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
my @name = qw /fred betty barney dino wilma pebbles bamm-bamm/;
-
my @index = <STDIN>;
-
-
chomp @index;
-
-
foreach my $i (@index){
-
print $name[$i], "\n";
-
}
能省则省。。。
- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
my @name = qw /fred betty barney dino wilma pebbles bamm-bamm/;
-
chomp (my @index = <STDIN>);
-
-
foreach (@index){
-
print $name[$_ - 1], "\n";
-
}
- chomp(@lines = <STDIN>);
-
@sorted = sort @lines;
-
print "@sorted\n";
阅读(329) | 评论(0) | 转发(0) |