簡體   English   中英

如何在Perl中從命令行讀取兩個文件?

[英]How can I read two files from command line in Perl?

一個人怎么能做到這一點? 我知道您必須使用<>。我已經嘗試過試一下,但這可能只是因為我在Windows上。

# run as:
#       perl my_script1.pl file1 file2
my ($file1, $file2) = @ARGV;
open my $fh1, '<', $file1;
open my $fh2, '<', $file2;
while (<$fh1>) {
    ... do something with $_ from $file1 ...
}
while (<$fh2>) {
    ... do something with $_ from $file2 ...
}
close $fh1;
close $fh2;

您沒有說明是否需要區別對待文件。 如果沒有,那么您可以使用<>而不需要顯式調用open

while (<>) {
  # Process $_
}

會很好的工作。 當到達第一個文件的末尾時, <>將繼續讀取第二個文件。 依此類推,直到用完所有要讀取的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM