Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The down-side of using references is that dereferencing stuff in multiple places can a) get messy; b) actually end up costing more time than simply duplicating the data and avoiding the dereferencing, if the data items are small.

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.

That observation if key here. If you don't need the values in the hash after you've copied them to the array, don't copy them, move them.

That can be achieved by deleteing the key/value pair from the hash and assigning the return from delete -- which is the value of the key being deleted -- to the array.

Like this:

my %hash = ('a'=>'test'); my @arr = delete $hash{'a'}; print $arr[0]."\n";

This results in the key 'a' being removed from the hash, and its former value 'test' being transfered directly to $arr[0] without any copying.

I assume that would be faster?

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.

delete also works with hash slices, which makes this very convenient and efficient for doing multiple transfers simultaneously:

%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" }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: push to array without copying by BrowserUk
in thread push to array without copying by chris212

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found