sumit_nagpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i have a shared hash in m threaded application. I want each element of this hash to contain an array. how to do it?
push(@{$my_hash{$val}},@{$arr_ref});
This gives the standard "Invalid value for shared scalar at .." error. Thanks Sumit

Edit by castaway, code tags by popular demand

Replies are listed 'Best First'.
Re: how to create shared hashes of arrays
by Grygonos (Chaplain) on Jul 09, 2004 at 12:42 UTC

    I would try doing it in a sample script first. without threads.. to make sure you are constructing the Data struct correctly.

    Firstly, your concept of an HoA is a little off. The hash doesn't contain an array.. the hash keys.. hash to array references as their value. for example:
    my $test = [1,2,3,4]; my %test = (array => $test); print $test{array}
    this should print something like
    ARRAY(0x2251e4)
    So to print an element of that array you would do
    print $test{array}->[0];
    Take a look at Perl Data Structures Cookbook @ perldoc.com
      i think i know that its the array reference which is stored in the as the value... i dont think i m facing any problems in that... its only the shared hash which is giving problems...
        got it!!! i was trying a few things and this one worked... $my_hash{$key} = &share([]); push(@{$my_hash{$key}},@{$arr_ref}); --Sumit
        From what I've read the shared hash should be almost no different.. so long as your constructing it correctly.. and what not.. post some sample code so we can see what's going on.