【发布时间】:2017-06-22 06:19:08
【问题描述】:
在我的代码中,我尝试在 while 循环条件中传递变量 $result。
我该怎么做?
我的代码:
use strict;
use warnings;
use File::Find::Rule;
my $output="/data/file.txt";
my @files=File::Find::Rule->file ->in($output);
foreach my $file(@files)
{
my ($file)=~m|(.*)|;
my $outfile="$file.txt";
open my $fh_out,'>',$outfile or die "error";
open my $fh,'<',$file or die "error";
while(my $line=<$fh>)
{
my @data=split/:/,$line);
for my $result(@data)
{
if(-f $outfile)
{
open my $fh1,'<',$result || die "$!";
$result=~s/("\S+|\S+")\s*/$1/g;
while($result)##Note:Error at this part $result from local variable should pass as loop condition.Then only it will print $result statement inside the while loop.Now the print $result is not printing.
{
$result.=$_;
print $result;
}
push @data,$fh1;
}
}
}
}
在我的代码@data 中,内容可以存储为局部变量$result。
现在应该在while 条件下使用相同的$result 内容。
我该怎么做?
文件句柄还有其他方法吗?
输入文件:$file
sync_logic : a
symbol1: b
symbol2: c
symbol3: d
@data会有以下内容
'sync_logic', 'a'
'symbol1', 'b'
'symbol2', 'c'
'symbol3', 'd'
$result 的预期输出应如下所示
sync_logic a
symbol1 b
symbol2 c
symbol3 d
错误:
I got the following error message when i run the above code:
Killed
【问题讨论】:
-
您使用
$result = "";行为$result分配一个空字符串。然后你试图从中读取?这是一个空字符串。它甚至不是一个有效的文件句柄。如果您的@data包含文件句柄,只需删除$result = "";看看会发生什么。如果@data确实 not 实际上有文件句柄,那么我不知道您要做什么,请解释一下。 -
你为什么要做
while ($result)或类似的事情?$result只是一个包含字符串的标量。只需执行for my $result( @data ){ print "result = $result\n" }而不是实际的for循环,它将打印$result的值。此外,您是否意识到@data从不包含两个以上的元素?另外,$out是什么? -
另外,我最近看到一两个类似的问题;请问您在学习教程/书籍时是否遇到此问题?如果有,是哪一个?
-
@salar33:好的,我已经整理了你的代码,以便我可以阅读它,很明显它不会编译。您从不声明
$output,您对File::Find::Rule的调用是错误的,并且您有一个无效的while ( ... ) { ... } else { ... }构造。请出示您的真实代码。 -
@salar33:不,你没有。您的代码仍然是随机的废话,甚至无法编译。
标签: perl