in reply to Re^3: What is the easiest way to merge two hashes?
in thread What is the easiest way to merge two hashes?

For this you have to create the image of the merged hash in memory. If you have incredibly large data structures ( as I do ) this may be a wasting of resources ( in the extreme case this can cause the program to run out of memory ). In may solution it is enough to query the keys of both hashes without ever creating the merged hash itself.
  • Comment on Re^4: What is the easiest way to merge two hashes?

Replies are listed 'Best First'.
Re^5: What is the easiest way to merge two hashes?
by BrowserUk (Patriarch) on Feb 15, 2015 at 16:37 UTC
    In may solution it is enough to query the keys of both hashes without ever creating the merged hash itself.

    Sorry, but that is rubbish. This code:

    my $joined_keys = [ keys( %{ { map { $_=>undef } keys( %{$hash1} ) , keys( %{$hash2} ) +} } ) ]; # This.....^.......................................................... +..^ *is a hash* built from #...............................^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^ #...the combined (potentially huge) lists of the keys from both hashes + PLUS #.....................^^^^^...undef as the value for each key in the c +ombined list...

    And is functionally and memory consumption-wise, nearly identical to this code:

    sub mergedHashKeys{ [ keys %{ { %{ $_[0] }, %{ $_[1] } + } } ] };;

    And, your code creates *two (potentially) huge lists*; one going into the map and one coming out that is twice as long; from which the combined hash is constructed.

    Any memory savings will be minimal.


    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". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked