function - can't use an undefined value as a symbol perl -


i'm trying write in file value taken array. i'm having error ''can't use undefined variables symbol reference at... line 81:

foreach $k (sort keys %{$value2}){     print $value4 $k." = ".%{$value2{$k}}. $value3;    sub printit{   $value1 = $_[0];#"ipadress" string   $value2 = $_[1];#%hash2   $value3 = $_[3];#"paquet" string   $value4 = $_[4];#source 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 please indicate me what's wrong?

the things code 1 , work fine. , didn't concatanate source , 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 lot of codes same thing wanted create function able reduce numbers of lines.

your forgot comma or concatenation here:

print $value4 $k." = ".%{$value2{$k}}. $value3; 

perl thinks want use $value4 has filehandle (symbol), , apparently $value4 undefined. reason undefined because assign value of $_[4] want $_[3] (since arrays zero-indexed.)

it looks intend $value4 file name output; if that's case need open file filehandle:

open $fh, '>', $value4 or die "could not open file $value4: $!"; ... print { $fh } $k." = ".%{$value2{$k}}. $value3; 

so have 3 things fix:

  1. figure out why $value4 undefined , fix that. (when find appending numbers onto names of scalars, chances want use array anyway. use @_ directly or grab values @args array rather bunch of scalars.)

  2. figure out how want format output string , use filehandle, not filename, print.

  3. figure out how want serialize hash referenced $value2{$k}, because printing hash in scalar context not want do.

(updated suggestions after realized you're lacking filehandle)


Comments