#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    References Array


    Hello All
    I need to create an array with references while each cell of the array pointing to his own array in a way - so I can loop this reference array get into the 5 arrays that he pointing on and push the same lines into the different arrays. I tried to do something like this:

    my(@arrA);
    my(@arrB);
    my(@arrC);
    my(@arrD);
    my(@arrE);

    my($ref_to_arrA) = \@arrA;
    my($ref_to_arrB) = \@arrB;
    my($ref_to_arrC) = \@arrC;
    my($ref_to_arrD) = \@arrD;
    my($ref_to_arrE) = \@arrE;

    @Ref_to_Arr = {$ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE} # I initial the reference array with a pointers to my 5 arrays

    my($moo);
    foreach $moo (@Ref_to_Arr){
    push(@Ref_to_Arr, "HELLO WORLD! ");
    } #I want to go all over the Ref_to_Arr array and with the help of the references to push my string into my 5 arrays - @arrA @arrB @arrC @arrD @arrE


    Now I expect, that if I will print the content of @arrA @arrB @arrC @arrD @arrE so I will see in every array the string: HELLO WORLD...

    But something with the syntax is wrong...

    Glad if you know how I can make it work...

    Thanks a lot in advance!!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    Your basic problem is that you've used the wrong type of brackets for creating your array of references. Curly brackets (braces) will create a reference to a hash. You want round brackets (parentheses) to create an array, like so:
    Code:
    my(@arrA);
    my(@arrB);
    my(@arrC);
    my(@arrD);
    my(@arrE);
    
    my($ref_to_arrA) = \@arrA;
    my($ref_to_arrB) = \@arrB;
    my($ref_to_arrC) = \@arrC;
    my($ref_to_arrD) = \@arrD;
    my($ref_to_arrE) = \@arrE;
    
    my @Ref_to_Arr = ($ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0
    Hi

    Thx for the answer ! it really was a syntex error but even after I corrected the prentsis it still dont work. I think that it gets into an infinite loop maybe it something in the foreach and push section that I did wrong?

    Thanks again!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    You're pushing the new value onto the wrong array.

    Those initial array declarations aren't needed.

    Code:
    # the choice of var names could be improved
    my $ref_to_arrA = [];
    my $ref_to_arrB = [];
    my $ref_to_arrC = [];
    my $ref_to_arrD = [];
    my $ref_to_arrE = [];
    
    @Ref_to_Arr = ($ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE);
    
    foreach my $moo (@Ref_to_Arr){
        push(@{$moo}, "HELLO WORLD! ");
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0
    Hi,

    Thanks a lot but still not working.
    I did what you said and it gave me this error for the line that starts with "push..":

    Scalar found where operator expected at HelloWorld.pl line 138, near "@($moo"
    (Missing operator before $moo?)
    syntax error at HelloWorld.pl line 138, near "@($moo"
    Execution of HelloWorld.pl aborted due to compilation errors.

    Do you know what this error means?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    You didn't copy the code correctly for the push statement.

    You used ($moo) parens around the var where you should have used {$moo} braces.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0
    WOW!! It works!!!! Thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo