用perl比较文件中的行 [英] Comparing lines in a file with perl

查看:84
本文介绍了用perl比较文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图比较两个文件之间的行和相同的匹配行.

Ive been trying to compare lines between two files and matching lines that are the same.

出于某种原因,下面的代码仅会通过'text1.txt'的第一行,并输出'if'语句,而不管两个变量是否匹配.

For some reason the code below only ever goes through the first line of 'text1.txt' and prints the 'if' statement regardless of if the two variables match or not.

谢谢

use strict;
open( <FILE1>, "<text1.txt" );
open( <FILE2>, "<text2.txt" );
foreach my $first_file (<FILE1>) {
    foreach my $second_file (<FILE2>) {
        if ( $second_file == $first_file ) {
            print "Got a match - $second_file + $first_file";
        }
    }
}
close(FILE1);
close(FILE2);

推荐答案

如果比较字符串,请使用eq运算符. "=="数值比较参数.

If you compare strings, use the eq operator. "==" compares arguments numerically.

这篇关于用perl比较文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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