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

In reply to copying array from a hash reference by Greg_R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.