Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: copying array from a hash reference

by AnomalousMonk (Abbot)
on Sep 28, 2011 at 01:19 UTC ( #928219=note: print w/ replies, xml ) Need Help??


in reply to copying array from a hash reference

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'


Comment on Re: copying array from a hash reference
Select or Download Code
Replies are listed 'Best First'.
Re^2: copying array from a hash reference
by Greg_R (Novice) on Sep 28, 2011 at 22:28 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2015-08-01 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls