Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Shared hash of anonymous arrays?

by KevinZwack (Chaplain)
on Aug 29, 2006 at 21:52 UTC ( [id://570262]=perlquestion: print w/replies, xml ) Need Help??

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

I've been able to create thread shared hashes of hashes and so forth... but the syntax to create a hash of anonymous arrays has me stumped. Getting this little snippet of code to work would be helpful. In fact, any help would be greatly appreciated.

Thanks,
Kevin Zwack

#!/usr/bin/perl5.8.8 -w use Data::Dumper; use threads; use threads::shared; my ($i, $j, $k) = (1, 2, 3); my %foo :shared; $foo{'bar'} = &share([]); #<-- Not what it wants $foo{'bar'} = [$i, $j, $k]; print Data::Dumper->new([\%foo],[qw(foo)])->Indent(4)->Quotekeys(1)->S +ortkeys(1)->Dump; exit;

Replies are listed 'Best First'.
Re: Shared hash of anonymous arrays?
by BrowserUk (Patriarch) on Aug 29, 2006 at 22:21 UTC

    You've flagged the wrong line as giving the error:

    c:\test>junk Invalid value for shared scalar at c:\test\junk.pl line 8.

    The line with the problem is $foo{'bar'} = [$i, $j, $k];

    Not $foo{'bar'} = &share([]); #<-- Not what it wants this one.

    The problem is that having successfully assigned an anonymous array (reference) to $foo{bar} by disabling the problematic protoype using &share( [] );, you then try to assign a new, completely different anonymous hash reference to it.

    Instead of this $foo{'bar'} = [$i, $j, $k]; which (unsuccessfully) attempts to overwrite the shared anonymous hash with a different non-shared anonymous hash with three values in it, you should assign the values through the shared reference like this

    @{ $foo{'bar'} } = ( $i, $j, $k );

    With that change, your snippet produces

    c:\test>junk $foo = { 'bar' => [ #0 '1', #1 '2', #2 '3' ] };

    You may find Re: Using threads::shared with multidimensional arrays helpful.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank-you all, that solved my problem!
Re: Shared hash of anonymous arrays?
by GrandFather (Saint) on Aug 29, 2006 at 22:04 UTC

    $foo{'bar'} is an array reference so the assignment to the array is:

    @{$foo{'bar'}} = ($i, $j, $k);

    The code then prints:

    $foo = { 'bar' => [ #0 '1', #1 '2', #2 '3' ] };

    DWIM is Perl's answer to Gödel
Re: Shared hash of anonymous arrays?
by mreece (Friar) on Aug 29, 2006 at 22:17 UTC
    note the synopsis from 'perldoc threads::shared' uses the :shared attribute or the share() function, but not both on the same variable.

    try removing the :shared attribute from the my %foo declaration.

    (do you intend to share the entire hash %foo or only $foo{bar}?)

    updated: i'm potentially way off base there, i am able to get it to 'work' (that is, stop producing the "Invalid value for shared scalar" error) using this approach or the others mentioned (@{$foo{'bar'}} = ($i, $j, $k);). it does seem plausible that you can't re-assign a new array-ref to a shared variable, but who knows what magic lurks beneath...? (not i!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://570262]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-25 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found