标签

2016年4月5日星期二

Perl: The first and last lines

1. Get the first and last lines

#!/usr/bin/perl -w
use strict;

print scalar <DATA>; # the first line
while (<DATA>) {
    print if eof; # the last line after loop
}


__DATA__
first
second
third
lastline

Output:

first
lastline

2. Get the first and last lines

#!/usr/bin/perl -w
use strict;

my $last;
my $first = <DATA>; # the first line

while (<DATA>) {
    $last = $_;  # the last line after loop
}
close DATA;

print "$first";
print "$last";


__DATA__
first
second
third
lastline

3. Skip the first line

readline <DATA>;
scalar <DATA>;
my $first = <DATA>;
@all = split /\s+/, <DATA>;

 

 

 

 

 

 

没有评论:

发表评论