Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl while循环不工作_Perl - Fatal编程技术网

perl while循环不工作

perl while循环不工作,perl,Perl,以下是我的文件,由delimeneter分离,并通过电子邮件发送 列表:设备1 |城市|街道|道路名称|地区|州|地区|国家|国家代码 ________________________________________________ Device1|City|Street|roadname|region|state|area|country|countrycode Device2|City|Street|roadname|region|state|area|country|countrycode

以下是我的文件,由delimeneter分离,并通过电子邮件发送 列表:设备1 |城市|街道|道路名称|地区|州|地区|国家|国家代码

________________________________________________
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 );

谢谢

我想你想要的是这样的:

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;
使用严格;
使用警告;

打开我的$INPUT,'你的问题真的不清楚。代码中没有while循环。感谢您的回复。。我尝试过做下面这样的事情,但不起作用…$Device=$arr[0]$条件=“$Device |未找到数据”;虽然(){if($line=~/$condition/){print$condition;$pattern=$condition}请返回并修改您的问题,使其包含整个代码,而不仅仅是选定的部分。还有,“不起作用”是什么意思?“不起作用”对于我们理解这个问题来说是不够的。你试的时候发生了什么?你得到了不正确的结果吗?你没有得到结果吗?如果结果不正确,是什么导致结果不正确?你在期待什么?你得到正确的结果了吗?如果是,它们是什么?别让我们猜了。谢谢你的回复,瑞克!!!我已经复制了你的代码,但它似乎与条件不匹配,并在else循环中打开我的$INPUT,'啊-这可能是因为行的结尾。我已经用
chomp
更新了代码以删除尾部的
/n
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;