Perl 如何打开/连接多个文件(取决于用户输入),然后同时使用两个文件

Perl 如何打开/连接多个文件(取决于用户输入),然后同时使用两个文件,perl,Perl,编辑:很抱歉误解,我已经编辑了一些东西,希望能真正要求我想要的 我想知道是否有办法打开/连接两个或多个文件以运行程序的其余部分 例如,“我的目录”包含以下文件: taggedchpt1_1.txt,parsedchpt1_1.txt,taggedchpt1_2.txt,parsedchpt1_2.txt等 程序必须同时调用标记和解析的。我想在chpt1_1和chpt1_2上运行这个程序,最好合并在一个.txt文件中,除非这样做非常慢。例如,运行两个文件可以完成的操作: taggedchpt1_1

编辑:很抱歉误解,我已经编辑了一些东西,希望能真正要求我想要的

我想知道是否有办法打开/连接两个或多个文件以运行程序的其余部分

例如,“我的目录”包含以下文件:

taggedchpt1_1.txt
parsedchpt1_1.txt
taggedchpt1_2.txt
parsedchpt1_2.txt

程序必须同时调用标记和解析的。我想在chpt1_1和chpt1_2上运行这个程序,最好合并在一个.txt文件中,除非这样做非常慢。例如,运行两个文件可以完成的操作:

taggedchpt1_1_和_chpt1_2
parsedchpt1_1_和_chpt1_2

这可以通过Perl实现吗?或者我应该自己组合文本文件(或者自动化这个过程,生成chpt1.txt,其中包括chpt1_1、chpt1_2、chpt1_3等等)

#/usr/bin/perl
严格使用;
使用致命警告=>“全部”;
打印“请在表格chp###U sec#:\n中键入章节号”##例如,用户输入31_3
chomp(我的$chapter_和_section=“chpt”);
打印“请键入搜索词:\n”;
chomp(我的$search_key=);

打开(我的$tag_语料库,“你就快到了……这比在每个文件上单独打开要高效一些

#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
print "Please type in the chapter and section NUMBERS in the for chp#_sec#:\n";
chomp (my $chapter_and_section = "chpt".<>);
print "Please type in the search word:\n";
chomp (my $search_key = <>);

open(FH, '>output.txt') or die $!;   # Open an output file for writing
foreach ("tagged${chapter_and_section}.txt", "parsed${chapter_and_section}.txt") {
    open FILE, "<$_" or die $!;      # Read a filename (from the array)
    foreach (<FILE>) {
       $_ =~ s/THIS/THAT/g;   # Regex replace each line in the open file (use 
                              #     whatever you like instead of "THIS" &
                              #     "THAT"
       print FH $_;           # Write to the output file
    }
}
!/usr/bin/perl
严格使用;
使用致命警告=>“全部”;
打印“请在for chp###U sec#中键入章节号:\n”;
chomp(我的$chapter_和_section=“chpt”);
打印“请键入搜索词:\n”;
chomp(我的$search_key=);
打开(FH,“>output.txt”)或die$!;#打开输出文件进行写入
foreach(“标记了${chapter\u和{u section}.txt”,“解析的${chapter\u和{u section}.txt”){

打开文件,“您就快到了……这比在每个文件上单独打开要高效一些

#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
print "Please type in the chapter and section NUMBERS in the for chp#_sec#:\n";
chomp (my $chapter_and_section = "chpt".<>);
print "Please type in the search word:\n";
chomp (my $search_key = <>);

open(FH, '>output.txt') or die $!;   # Open an output file for writing
foreach ("tagged${chapter_and_section}.txt", "parsed${chapter_and_section}.txt") {
    open FILE, "<$_" or die $!;      # Read a filename (from the array)
    foreach (<FILE>) {
       $_ =~ s/THIS/THAT/g;   # Regex replace each line in the open file (use 
                              #     whatever you like instead of "THIS" &
                              #     "THAT"
       print FH $_;           # Write to the output file
    }
}
!/usr/bin/perl
严格使用;
使用致命警告=>“全部”;
打印“请在for chp###U sec#中键入章节号:\n”;
chomp(我的$chapter_和_section=“chpt”);
打印“请键入搜索词:\n”;
chomp(我的$search_key=);
打开(FH,“>output.txt”)或die$!;#打开输出文件进行写入
foreach(“标记了${chapter\u和{u section}.txt”,“解析的${chapter\u和{u section}.txt”){

打开文件,“您可以迭代文件名,依次打开和读取每个文件名。或者您可以生成一个迭代器,知道如何从文件序列中读取行

sub files_reader {
    # Takes a list of file names and returns a closure that
    # will yield lines from those files.
    my @handles = map { open(my $h, '<', $_) or die $!; $h } @_;
    return sub {
        shift @handles while @handles and eof $handles[0];
        return unless @handles;
        return readline $handles[0];
    }
}

my $reader = files_reader('foo.txt', 'bar.txt', 'quux.txt');

while (my $line = $reader->()) {
    print $line;
}
子文件\u读取器{
#获取文件名列表并返回一个
#将从这些文件中生成行。

my@handles=map{open(my$h),您可以迭代文件名,依次打开和读取每个文件名。或者您可以生成一个迭代器,它知道如何从文件序列中读取行

sub files_reader {
    # Takes a list of file names and returns a closure that
    # will yield lines from those files.
    my @handles = map { open(my $h, '<', $_) or die $!; $h } @_;
    return sub {
        shift @handles while @handles and eof $handles[0];
        return unless @handles;
        return readline $handles[0];
    }
}

my $reader = files_reader('foo.txt', 'bar.txt', 'quux.txt');

while (my $line = $reader->()) {
    print $line;
}
子文件\u读取器{
#获取文件名列表并返回一个
#将从这些文件中生成行。
我的@handles=map{open(我的$h,还没有人提到这个黑客?好的,在这里

{
    local @ARGV = ('taggedchpt1_1.txt', 'parsedchpt1_1.txt', 'taggedchpt1_2.txt',  
                   'parsedchpt1_2.txt');
    while (<ARGV>) {
       s/THIS/THAT/;
       print FH $_;
    }
}
{
local@ARGV=('taggedchpt1_1.txt','parsedchpt1_1.txt','taggedchpt1_2.txt',
“parsedchpt1_2.txt”);
而(){
这个/那个/;
打印FH$;
}
}
ARGV
是一个特殊的文件句柄,它可以遍历
@ARGV
中的所有文件名,关闭一个文件并根据需要打开下一个文件。通常
@ARGV
包含传递给
perl
的命令行参数,但您可以将其设置为任何您想要的值。

还没有人提到这种攻击吗?好的,给你

{
    local @ARGV = ('taggedchpt1_1.txt', 'parsedchpt1_1.txt', 'taggedchpt1_2.txt',  
                   'parsedchpt1_2.txt');
    while (<ARGV>) {
       s/THIS/THAT/;
       print FH $_;
    }
}
{
local@ARGV=('taggedchpt1_1.txt','parsedchpt1_1.txt','taggedchpt1_2.txt',
“parsedchpt1_2.txt”);
而(){
这个/那个/;
打印FH$;
}
}

ARGV
是一个特殊的文件句柄,它迭代
@ARGV
中的所有文件名,关闭一个文件并根据需要打开下一个文件。通常
@ARGV
包含传递给
perl
的命令行参数,但您可以将其设置为任何需要的参数。

是否省略了打开的参数(我的$parse_corpus…是故意的吗?谢谢你的回答,我会看看我是否能实现它。@Jon,我是故意这么做的。
foreach
遍历文件名数组,并按顺序打开每个文件名。…编写替换输出(不知道你想要什么),一个名为
output.txt
的文件名。实际上,我在其中留下了一行可能已被删除的内容。感谢您的关注,嗯,我仍然可以使用它同时打开标记和解析的内容吗?还是我只需使用两个foreach循环,一个用于标记DCHPT…另一个用于解析DCHPT…?然后使用我编辑的问题中的最后两行?谢谢!@Jon,如果您的perl程序是单线程的,那么您是否同时打开一个或两个文件并不重要(正如您的帖子所示)。如果要同时处理,则需要为每个要处理的文件生成线程。这比您可能需要的复杂得多。foreach循环一次打开一个文件,并根据文件的内容更改行regexp@Mike彭宁顿:很抱歉我不理解,但我确实需要$tag_和$corpusd$parse_语料库同时使用我的程序,你的答案可以这样做吗?谢谢你的时间,你是否遗漏了开放(我的$parse_corpus…是故意的吗?谢谢你的回答,我会看看我是否能实现它。@Jon,我是故意这么做的。
foreach
遍历文件名数组,并按顺序打开每个文件名。…编写替换输出(不知道你想要什么),一个名为
output.txt
的文件名。实际上,我在其中留下了一行可能已被删除的内容。感谢您捕捉到了这一点。嗯,我仍然可以使用它同时打开标记和解析的文件吗?还是只使用