我只是想学习一些Perl,所以我正在通过一个函数来掌握语言。有人可以向我解释一下这个函数到底在做什么吗?
#! /usr/bin/perl
use strict;
my %hash;
&Parse('first.txt');
&Parse('second.txt');
my $outputpath = 'output.txt';
unlink ($outputpath);
open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!";
print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash);
close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!";
sub Parse {
my $inputpath = shift;
open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!";
while (<INPUT>) {
chomp;
my @row = split(/\t/, $_);
my $col1 = $row[0];
shift @row;
push(@{$hash{$col1}}, @row);
}
close (INPUT) || die "Failed to close INPUT ($inputpath) - $!";
return 1;
}
我对shift
和push
以及chomp
更感兴趣。
答案 0 :(得分:4)
编辑:您发布了一些额外的代码,我也会对此进行评论。
#!/usr/bin/perl
#The first line (has to be first, hence this comment comes after) allows the linux shell to know
#this is a perl program, and to call perl to execute it.
#use strict: allow stricter checking of perl syntax. You should always do this.
use strict;
#declare a global variable called hash - not a very good name...
my %hash;
#call the method with 'first.txt' as argument
&Parse('first.txt');
&Parse('second.txt'); #same thing, different parameter
my $outputpath = 'output.txt';
#destroy the file declared above if it exists
unlink ($outputpath);
# open the file for append (could simple have opened for output and not used the unlink above...)
open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!";
#print a line to output
#the line comes from a foreach loop
#the foreach loop runs over the hash, sorted by key
#each hash entry contains an array, this array is converted by a string using the JOIN function
# the join function will paste the elements of the array into a string, seperated by a tab
print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash);
#Close the outputfile
close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!";
这个程序可能是前段时间写的 - 现代Perl看起来有点不同,并且有一些尚未到达的最佳实践。
不要以此为例说明如何编写Perl。如果你笑得很好,也许Ether会为你改写这个:)
#declare a sub
sub Parse {
# the parameters given to a sub are stored in @_.
#shift without arguments takes the first element from @_
my $inputpath = shift;
#opens the file "inputpath" into fileglob INPUT.
#If this fails, quit with an error message
open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!";
#loop over the file one line at the time, putting each line in $_
while (<INPUT>) {
#chop = remove last character. chomp = remove last character if it is a CRLF.
#Without arguments, works on $_
chomp;
#split the $_ variable (containing the row)
#into an array based on the tab character
my @row = split(/\t/, $_);
# take the first element into col1
my $col1 = $row[0];
# shift it (remove the first element)
shift @row;
# actually both rows above can be just written as one statement:
my $col1 = shift @row;
#the $hash variable is probably a global hashref defined somewhere above...
#the $hash hashref now contains a bucket named with the 'col1' value
# the value of that bucket is the array of the row we just read
push(@{$hash{$col1}}, @row);
# end the while loop
}
#close the file or die
close (INPUT) || die "Failed to close INPUT ($inputpath) - $!";
#end the method
return 1;
}
答案 1 :(得分:3)
答案 2 :(得分:2)
如果你有一个健全的perl安装,以下命令行命令将有所帮助:
perldoc -f shift
perldoc -f push
perldoc -f chomp
你也会喜欢:
perldoc perlfunc
perldoc perlvar
不要错过关于perlvar
的{{1}}部分,否则你将无法获得perl的内容。
你会逐渐注意到perl不是面向对象的,它支持对象,但这是一个非常奇怪的实现。 Perl更多完成工作,工作通常与某种数据集的提取或翻译有关。
Perl one liners是你将要编写的最强大的命令行:
$_
查看perl -pe 's/(\d*)/$1*10/ge'
-p
,-e
,-n
和-i
开关
(这是Perl 6被安排作为重大改写的主要原因之一,现在只有在始终并且计划在 Duke Nukem Forever )
无论如何,{p>perldoc perlrun
就像是python的shift
或javascript的some_array.pop(1)
等。
some_array.shift()
就像python的push
或javascript的some_array.append(junk)
等。
some_array.push(more_junk)
非常奇特,实际上是chomp
的跨平台版本:它从已从stdin读取的行中删除行尾字符。克服这个小钻石操作员&lt;&gt;这是一种破解。 (检查chop
- “I / O操作符”部分)缺陷:diamond逐行读取stdin或命令行文件参数,但它不会删除perldoc perlop
。 (也不是\n
)
\r\n
删除它们。 (chomp
仅移除chop
并单独留下\n
。)