i have a problem where I need to have an array as a value in an associative array.
Go through the code below. Here I am trying to loop the files in a directory and it is more likely that more than 1 file can have the same ctrno
. So, I would like to see what are all the files having the same ctrno
. The code below gives error at "$ctrno_hash[$ctrno] = @arr;
" in the else condition. The same case would be for if condition as well.
Am I following the right approach or could it be done differently?
sub loop_through_files
{
$file = "@_";
open(INPFILE, "$file") or die $!;
#print "$file:$ctrno\n";
while (<INPFILE>)
{
$line .= $_;
}
if ($line =~ /$ctrno/ )
{
print "found\n";
if ( exists $ctrno_hash[$ctrno])
{
local @arr = $ctrno_hash[$ctrno];
push (@arr, $file);
$ctrno_hash[$ctrno] = @arr;
}
else
{
local @arr;
push(@arr, $file);
$ctrno_hash[$ctrno] = @arr;
}
}
}