不能使用未定义的值作为符号perl

编程入门 行业动态 更新时间:2024-10-23 22:25:33
本文介绍了不能使用未定义的值作为符号perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在文件中写入从数组中取得的一些值。但是我有一些错误''不能使用一个未定义的变量作为符号参考...第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 数组而不是一堆标量。)

  • 找出你想如何格式化输出字符串,并为 print 使用文件句柄,而不是文件名。 找出你想要的方式以序列化由 $ value2 {$ k} 引用的散列,因为在标量上下文中打印散列几乎肯定不是您想要做的。

    (在我意识到您缺少文件句柄后更新了建议)

    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

    本文发布于:2023-11-05 09:59:49,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:符号   未定义   perl

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!