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

Hi,
I'm soooo stuck please help me. I've been trying to share this array forever but it does not work for me. :(
I need to make this work but I have to use "share" here, I can not simply return the variable. any idea how I can fix this?

use threads::shared; use strict; use warnings; use Data::Dumper; my $tmp=meow(); #my @state : shared; #if I use this line instead I get this "inva +lid value for shared scalar. (since state is an array of arrays)" my @state = &share([]); for (my $i = 0;$i <6 ; ++$i) { my $row = &share([]); @{$state[$i]} = $row; } print Dumper(@state); sub meow { my $i = 0; my $j = 1; my @p = ("grey" , "striped" , "purrr", "cutest_kitty" , "meowing_kitty", "the_lord_of_the_cats"); my @k = ("smart_kitty" , "striped_sweety" , "purrr", "meowing_poes" , "social_kitty", "the_lord_of_the_poezen"); for (my $f=0; $f<6 ; $f++) { push(@{$state[$i]}, $p[$f]); push(@{$state[$j]}, $k[$f]); }

and here is my output when I run this

~/my_perl_test> perl sharingpoes2.pl $VAR1 = [ [] ]; $VAR2 = [ [] ]; $VAR3 = [ [] ]; $VAR4 = [ [] ]; $VAR5 = [ [] ]; $VAR6 = [ [] ];

I really really appreciate all your help.
thanks a lot

Replies are listed 'Best First'.
Re: sharing array please help
by BrowserUk (Patriarch) on Aug 15, 2012 at 00:06 UTC
    1. First you call meow() which populates an array called @state.
    2. Then you initialise @state with an empty array reference:
      my @state = &share([]);
    3. Then you loop 6 times pushing empty array references into @state:
      for (my $i = 0;$i <6 ; ++$i) { my $row = &share([]); @{$state[$i]} = $row; }
    4. Then you dump the array print Dumper(@state); and all it contains are the empty array references.

    Can you really not see why?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: sharing array please help
by aitap (Curate) on Aug 15, 2012 at 06:48 UTC
    What's the purpose of $tmp variable? Where does meow sub end? Your code example is not complete.
    Sorry if my advice was wrong.
Re: sharing array please help
by BrowserUk (Patriarch) on Aug 14, 2012 at 23:09 UTC

    Have you heard of <code></code> tags?

    If and when you start using them, you may start getting answers to your questions.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      I actually did paste before and after my code... but nothing changed when I clicked on "preview". Sorry about that, but I'm new and I'm not sure how to fix it =(
        I actually did paste before and after my code...

        You "did paste" what?

        Read Writeup Formatting Tips.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: sharing array please help
by kcott (Archbishop) on Aug 15, 2012 at 19:51 UTC

    You haven't stated your goal and, as already pointed out, your code is incomplete: we can only guess at what you're trying to achieve. The following code populates @state:

    #!/usr/bin/env perl use strict; use warnings; use threads; use threads::shared; use Data::Dumper; my @state = &share([]); meow(); print Dumper \@state; sub meow { my @p = qw{grey striped purrr cutest_kitty meowing_kitty the_lord_of_the_cats}; my @k = qw{smart_kitty striped_sweety purrr meowing_poes social_kitty the_lord_of_the_poezen}; for (0 .. 5) { push @{$state[0]}, $p[$_]; push @{$state[1]}, $k[$_]; } return; }

    Output:

    $ pm_thr_share_array.pl $VAR1 = [ [ 'grey', 'striped', 'purrr', 'cutest_kitty', 'meowing_kitty', 'the_lord_of_the_cats' ], [ 'smart_kitty', 'striped_sweety', 'purrr', 'meowing_poes', 'social_kitty', 'the_lord_of_the_poezen' ] ];

    I have no idea if that's what you want but it might provide some help.

    Please read How do I post a question effectively? and follow its guidelines if you choose to post here again.

    -- Ken