我试图在文件中写入从数组中取得的一些值。但是我有一些错误''不能使用一个未定义的变量作为符号参考...第81行:
foreach $ k(sort keys%{$ value2}){ print $ value4 $ k。=。%{$ value2 {$ k}}。 $值3; sub printit { $ value1 = $ _ [0];#ipadress是一个字符串 $ value2 = $ _ [1 ];#%hash2 $ value3 = $ _ [3];#paquet是一个字符串 $ value4 = $ _ [4];#SOURCE是文件名 foreach $ k(sort keys%{$ value2}){ print $ value4 $ k。=。%{$ value2 {$ k}}。 $值3; if(%{$ value2 {$ k}}> = 2){print $ value4s;} print $ value4\\\; } } printit('ipadress',\%hash2,'paquet'。SOURCE);有人能指出我有什么问题吗? <事情是我的代码是这个,它工作正常。
print SOURCEIpadress#2 \\\\\\ ; foreach $ k(sort keys%hash2){ print SOURCE $ k。=。$ hash2 {$ k}。paquet; if($ hash2 {$ k}> = 2){print SOURCEs;} print SOURCE\\\; }但是我有很多代码可以做同样的事情,所以我想要创建一个可以减少行数的函数。
解决方案您在这里忘记了逗号或连接:
print $ value4 $ k。=。%{$ value2 {$ k}}。 $值3;Perl认为您想要使用 $ value4 有一个文件句柄(符号),显然 $ value4 是未定义的。它未定义的原因是因为您将其赋值为 $ _ [4] ,但您可能希望 $ _ [3] (因为数组是零索引的。)
它看起来像你打算 $ value4 是一个文件名称作为您的输出;如果是这种情况,那么你需要真正打开该文件来获取文件句柄:
打开我的$ fh,'>' ,$ value4或死无法打开文件$ value4:$!; ... print {$ fh} $ k。=。%{$ value2 {$ k}}。 $值3;所以你有三件事需要解决:
找出为什么 $ value4 未定义并解决该问题。 (当你发现自己将数字附加到标量名称上时,你可能想要使用一个数组,你可以直接使用 @_ 或者将值转换为 @args 数组而不是一堆标量。)
(在我意识到您缺少文件句柄后更新了建议)
I'm trying to write in file some value taken from an array. But I'm having some error ''Can't use an undefined variables as a symbol reference at... line 81:
foreach $k (sort keys %{$value2}){ print $value4 $k." = ".%{$value2{$k}}. $value3; sub printit{ $value1 = $_[0];#"ipadress" is a string $value2 = $_[1];#%hash2 $value3 = $_[3];#"paquet" is a string $value4 = $_[4];#SOURCE is the file name foreach $k (sort keys %{$value2}){ print $value4 $k." = ".%{$value2{$k}}. $value3; if (%{$value2{$k}} >= 2) { print $value4 "s";} print $value4 "\n"; } } printit('ipadress', \%hash2, ' paquet'. SOURCE );Could someone please indicate me what's wrong?
the things is my code is this one and it work fine. And I didn't concatanate SOURCE and it's still working fine.
print SOURCE "Ipadress #2\n\n"; foreach $k (sort keys %hash2){ print SOURCE $k." = ".$hash2{$k}." paquet"; if ($hash2{$k} >= 2) { print SOURCE "s";} print SOURCE "\n"; }but I'm having a lot of codes that does the same thing so I wanted to create a function to be able reduce the numbers of lines.
解决方案Your forgot a comma or concatenation here:
print $value4 $k." = ".%{$value2{$k}}. $value3;Perl thinks you want to use $value4 has a filehandle (symbol), and apparently $value4 is undefined. The reason it is undefined is because you assign it the value of $_[4] but you probably want $_[3] (since arrays are zero-indexed.)
It looks like you intend $value4 to be a file name for your output; if that's the case then you need to actually open that file to get a filehandle:
open my $fh, '>', $value4 or die "Could not open file $value4: $!"; ... print { $fh } $k." = ".%{$value2{$k}}. $value3;So you have three things to fix:
Figure out why $value4 is undefined and fix that. (When you find yourself appending numbers onto the names of scalars, chances are you probably want to use an array anyway. You could use just @_ directly or grab the values into an @args array rather than a bunch of scalars.)
Figure out how you want to format your output string and use a filehandle, not a filename, for print.
Figure out how you want to serialize the hash referenced by $value2{$k}, because printing a hash in scalar context is almost certainly not what you want to do.
(Updated suggestions after I realized you're lacking a filehandle)
更多推荐
不能使用未定义的值作为符号perl
发布评论