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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.