用制表符/空格分隔输出:Perl [英] Separating an Output with a Tab / Space : Perl

查看:144
本文介绍了用制表符/空格分隔输出:Perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理三个文本文档.第一个是主输入(Input 1),单词和单词类型(名词、动词等)用制表符分隔.

I am working with three text documents . The first one is the main input (Input 1) with words and the word type (Noun , Verb etc.) separated by a tab.

输入 1

John    N
goes    V
to      P
school  N
.       S
Mary    N
comes   V
from    P
home    N
.       S

第二个和第三个输入文本文件如下所示:

The second and third input text files look like this :

输入 2

John
Mary

输入 3

to
from

我的目标是将第二个和第三个文本文件与主要输入进行比较和匹配,并获得如下输出:

My objective is to compare and match the second and third text files with the main input and get a output like this :

预期输出:

John    N   N
goes    V
to      P   P
school  N
.       S
Mary    N   N
comes   V
from    P   P
home    N
.       S

所以,基本上,所有三列都应该用制表符或空格分隔.但是,我得到了这样的输出:

So, basically, all the three columns should be separated by tab or space. However, I am getting an output like this :

John N  
N
goes    
V
to P    
P
school  
N
.   
S
Mary N  
N
comes   
V
from P  
P
home    
N
.   
S

我相信这是在我将第一个文本文件的输入放入数组并打印值时发生的.请建议我一种获得所需输出的方法.我使用的程序编码如下:

I believe this is happening as I took the input of the first text file into an array and printed the values.Please suggest me a way to get the desired output. The program coding that I have used is below :

#!/usr/bin/perl

use warnings;
use strict;

my @file = ('Input 1.txt');

open my $word_fh, '<', 'Input 2.txt' or die $!;
open my $word2_fh, '<', 'Input 3.txt' or die $!;

my %words_to_match = map {chomp $_; $_ => 0} <$word_fh>;
my %words_to_match2 = map {chomp $_; $_ => 0} <$word2_fh>;

close $word_fh;
close $word2_fh;

check($_) for @file;

sub check {
    my $file = shift;

open my $fh, '<', $file or die $!;

while (<$fh>){
    chomp;
    my @words_in_line = split;

    for my $word (@words_in_line){
        $word =~ s/[(\.,;:!)]//g;
        $word .= '  N' if exists $words_to_match{$word};
        $word .= '  P' if exists $words_to_match2{$word};

        print "$word\n";
    }
    print "\n";
}

再次,目标是输出所有三列以制表符或空格分隔.任何提示或建议表示赞赏.提前致谢.

Again ,the objective is to have an output with all the three columns separated by tab or space. Any hints or suggestions is appreciated. Thanks in advance.

推荐答案

您输出了一个不必要的换行符,并且您错误地构建了新的输出行.无需在哈希中搜索类型"列.这将产生所需的输出.

You are outputting an unnecessary newline, and you are constructing your new output line incorrectly. There is no need to search your hashes for the "type" column. This produces the desired output.

use warnings;
use strict;

my @file = ('Input 1.txt');

open my $word_fh,  '<', 'Input 2.txt' or die $!;
open my $word2_fh, '<', 'Input 3.txt' or die $!;

my %words_to_match  = map { chomp $_; $_ => 0 } <$word_fh>;
my %words_to_match2 = map { chomp $_; $_ => 0 } <$word2_fh>;

close $word_fh;
close $word2_fh;

check($_) for @file;

sub check {
    my $file = shift;
    open my $fh, '<', $file or die $!;
    while (<$fh>) {
        chomp;
        my ($word, $type) = split;
        my $line = $_;
        $line .= '  N' if exists $words_to_match{$word};
        $line .= '  P' if exists $words_to_match2{$word};
        print "$line\n";
    }
}

这篇关于用制表符/空格分隔输出:Perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆