-1

下面是我用分隔符分隔并通过电子邮件列表进一步发送的文件:Device1|City|Street|roadname|region|state|area|country|countrycode

________________________________________________
Device1|City|Street|roadname|region|state|area|country|countrycode
Device2|City|Street|roadname|region|state|area|country|countrycode
Device3|No data found
Device4|No data found
_________________________________________________
my $filename = '/tmp/list.txt';
open my $ifh, '<', $filename
  or die "Cannot open '$file' for reading: $!";
local $/ = '';
my $filename = <$ifh>;

my @arr = split(/\|/, $filename , -1);
$Device = $arr[0];
$Region = $arr[2];
$State = $arr[3];
$area = $arr[10];
$country = $arr[19];

$logger->debug("$logid >> file information Device Name: $Device");
$logger->debug("$logid >> file information Region: $Region");
$logger->debug("$logid >> file information State: $State");
$logger->debug("$logid >> file information Area: $area");
$logger->debug("$logid >> file information Country: $country");

close( $ifh );

我能够获得以下信息,但我的要求是每当在显示“未找到数据”的行中将其分配给变量,例如..“模式”,我将通过电子邮件进一步发送。

$smtp->datasend("$Device1|$region|$state|$area|$country\n");
$smtp->datasend("$pattern\n");

谢谢

4

1 回答 1

1

我认为你想要的是这样的:

use strict;
use warnings;
open my $INPUT, '<', '/tmp/list.txt' or die $!;
while (<$INPUT>) {
    chomp;
    my ($device, $data) = split(/\|/, $_, 2);
    if ($data eq 'No data found') {
        # Do whatever you need to do when there is no data
    } else {
        my @values = split(/\|/, $data);
        my ($region, $state, $area) = @values[3,4,5];
        # Further processing as needed
    }
}
close $INPUT;

几点注意事项:

  • 总是use strict-use warnings它会为你解决很多问题。my $filename就像您声明两次的事实一样。

  • 第三个参数split是可选的,只有当它是正数时才有意义。

  • 您可能正在设置$/ = ''一次 slurp 整个文件,但您想逐行处理它。

于 2013-04-11T16:43:16.703 回答