Storing array as value in associative array
        Posted  
        
            by Jagannath
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jagannath
        
        
        
        Published on 2010-05-11T09:06:48Z
        Indexed on 
            2010/05/11
            9:14 UTC
        
        
        Read the original article
        Hit count: 342
        
perl
i have a problem where I need to have an array as value in 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 diffently?
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;
       }
    }
}
        © Stack Overflow or respective owner