note
BrowserUk
<p>The down-side of using references is that dereferencing stuff in multiple places can a) get messy; b) actually <i>end up costing more time than simply duplicating the data</I> and avoiding the dereferencing, <b>if the data items are small.</b>
<blockquote><i>The hash won't be used once the array is created, so I'm not concerned about the hash values being modified when the array values are.</i></blockquote>
<p>That observation if key here. If you don't need the values in the hash after you've copied them to the array, <b>don't copy them, move them.</b>
<P>That can be achieved by [delete]ing the key/value pair from the hash and assigning the return from [delete] -- which is the value of the key being [delete]d -- to the array.
<P>Like this:<code>
my %hash = ('a'=>'test');
my @arr = delete $hash{'a'};
print $arr[0]."\n";
</code>
<p>This results in the key 'a' being removed from the hash, and its former value 'test' being transfered directly to <c>$arr[0]</c> without any copying.
<blockquote><i>I assume that would be faster?</i></blockquote>
<p>It will be; provided: a) the size of the values is sufficient to make a noticeable difference -- a few hundred bytes would do it -- and b) it doesn't force you to do too much extra work other places as a result.
<P>[delete] also works with hash slices, which makes this very convenient and efficient for doing multiple transfers simultaneously:<code>
%h = qw[ the quick black fiend jumps over the lazy god child ];;
pp \%h;;
{ black => "fiend", god => "child", jumps => "over", the => "lazy" }
print delete @h{ qw[ the black god ] };;
lazy fiend child
pp \%h;;
{ jumps => "over" }
</code>
<div class="pmsig"><div class="pmsig-171588">
<hr />
<font size=1 >
<div>With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'</div>
<div>Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.</div>
<div>"Science is about questioning the status quo. Questioning authority". [http://www.theregister.co.uk/2016/11/02/complexity_in_it/|The enemy of (IT) success is complexity.]</div>
<div>In the absence of evidence, opinion is indistinguishable from prejudice. </div>
</font>
</div></div>
1175917
1175917