遍历文件时检查EOF

时间:2018-09-11 18:36:57

标签: perl eof

我正在逐行读取文件15inv.txt。我从每一行获取“项目编号”,然后打开另一个文件active.txt并搜索匹配的项目编号。

如果找到匹配项,我想将其打印到results.txt并附加“ matched”。如果我到达文件末尾但找不到,请打印出“未达到匹配的EOF”。

我正在尝试检查15inv.txt中的项目编号是否在active.txt中。

15inv.txt文件如下所示。该文件可以具有多个项目编号。

1 5,I413858,O313071 ,2015-5-11 12:01:01,10033,WHITE HOUSE FURNITURE                   ,FAIRFIELD           ,NJ,29562,1,460,460

active.txt文件中包含项目号,并且仅显示一次。

30-18
30-46
26817

我的代码在哪里出错?

#!/usr/bin/perl -w

$inv = '15inv.txt';
open INV, "<$inv" or die "Unable to open the file\n";

$inv_out = '15inv-OBS.csv';
open INVOUT, ">$inv_out" or die "Unable to open the file\n";

$count = 0;
print INVOUT "Item #, Qty, Cost, Ext Cost, Status \n";

while ( <INV> ) {

    $inv_line = $_;
    chomp($inv_line);
    $count++;

    ($inv_rep, $inv_invoice, $inv_order, $inv_date, $inv_account, $inv_name, $inv_city, $inv_state, $inv_item, $inv_qty, $inv_cost, $inv_ecost)  = split(/,/, $inv_line);

    $inv_item =~ s/\s+//;  # remove spaces

    $active = 'active.txt'; # active items
    open ACTIVE, "<$active" or die "Unable to open the file\n";

    while ( <ACTIVE> ) {

        $the_active = $_;
        chomp($the_active);

        $active_item = substr($the_active, 0,10);

        $active_item =~ s/\s+//;
        next if ( $inv_item ne $active_item );

        if ( $inv_item eq $active_item ) {
            print INVOUT "$inv_item, $inv_qty, $inv_cost,$inv_ecost,IN \n";
            next;
        } # end of if 

    } # end of ACTIVE while loop

    print INVOUT "$inv_item, $inv_qty, $inv_cost,$inv_ecost, EOF \n";

} # end of INV while loop

print "Done!!! \n";

close FILE;
close INV;
close INVOUT;

exit;

0 个答案:

没有答案