Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

copying array from a hash reference

by Greg_R (Novice)
on Sep 28, 2011 at 00:36 UTC ( #928211=perlquestion: print w/ replies, xml ) Need Help??
Greg_R has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write various messages across multiple lines based on the current state of my subroutine. These messages need to be placed into an array to be returned to the calling program (it has to be an array due to restrictions of the calling program). I'm using hashes of arrays to define the possible message types and want to copy one of the arrays based on a hash reference into a local array variable. The below code snippet is a generic version of what I'm trying to do:
%AniType = (cats => ["I", "like", "cats"], dogs => ["I", "like", "dogs"], none => ["I", "like", "neither"], both => ["I", "like", "both"], ); if ($likecats) {@AniAry = $AniType{cats} } elsif ($likedogs) {@AniAry = $AniType{dogs} } elsif ($likeboth) {@AniAry = $AniType{both} } else {@AniAry = $AniType{none} } foreach $line (0..2) { print "$AniAry[$line] MiscArya[$line] MiscAryb[$line] \n"; }
The problem is that @AniAry = $AniType{cats} does not result in a copy of the array. I've tried the following as well:
@AniAry = @{ $AniType{cats} }; $AniAry = $AniType{cats};
The 2nd option works if I use $AniAry->[0] but that's a reference, not an actual copy like I require. Any help is appreciated, thanks! Greg

Comment on copying array from a hash reference
Select or Download Code
Re: copying array from a hash reference
by onelesd (Pilgrim) on Sep 28, 2011 at 00:50 UTC

    You will save yourself a lot of grief by putting use strict; use warnings; at the top of your script and then fixing the errors.

    What output are you expecting?

Re: copying array from a hash reference
by AnomalousMonk (Abbot) on Sep 28, 2011 at 01:19 UTC
    The problem is that @AniAry = $AniType{cats} does not result in a copy of the array. I've tried the following as well:
        @AniAry = @{ $AniType{cats} };
        ...

    I don't understand what you mean in the second code example above. This works (i.e., copies the referenced array) for me:

    >perl -wMstrict -le "my %AniType = ( cats => ['I', 'like', 'cats'], dogs => ['I', 'like', 'dogs'], none => ['I', 'like', 'neither'], both => ['I', 'like', 'both'], ); ;; my @AniAry = @{ $AniType{cats} }; printf qq{'$_' } for @AniAry; " 'I' 'like' 'cats'
      You experienced the same confusion that I did! I thought I was copying the array correctly (based on some perldocs examples). I added some additional debug code and determined that the program calling my subroutine is extremely picky in terms of the format that I return. In fact, my best option was to create and return a long string for each display line from the referenced hashes. Thanks for everyone's help; knowing I was copying / referencing correctly allowed me to focus on other areas of the code and catch the real problem.
Re: copying array from a hash reference
by davido (Archbishop) on Sep 28, 2011 at 02:37 UTC

    You need to dereference the array-ref if you're going to copy it to a new array:

    @AniAry = @{$AniType{cats}}

    ...for example. perlref, perlreftut, perldsc are good sources of information. If you plan to use Perl, they're worth investing the time reading.


    Dave

Re: copying array from a hash reference
by ikegami (Pope) on Sep 28, 2011 at 03:13 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-12-26 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls