CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
perl: restrict perl from automaticaly creating a hash branches on check

 

First page Previous page 1 2 Next page Last page  View All


Laurent_R
Veteran / Moderator

Nov 10, 2014, 1:55 AM

Post #26 of 30 (3117 views)
Re: [alex5161] perl: restrict perl from automaticaly creating a hash branches on check [In reply to] Can't Post

If the final interesting piece of data is always at the same level of nestedness, or always beyond a certain level, what you could do it to have an auxilliary hash having more or less the same content as your main hash, but in which you would not care about autovivification of lower level hash references. You could then do the lookup directly in the auxilliary hash at the chosen level (lower levels will autovivify, but you don't care, because the chosen level will not). Maintaining the two hashes in parallel is certainly a bit tricky and has an overhead, but it can probably be done and bring some significant performance improvement.


Laurent_R
Veteran / Moderator

Nov 10, 2014, 5:38 AM

Post #27 of 30 (3114 views)
Re: [alex5161] perl: restrict perl from automaticaly creating a hash branches on check [In reply to] Can't Post

The other solution you might consider, rather than having a deeply nested hash, is to use a flat hash in which the keys would be a concatenation of all the keys you currently have in the above level.

Rather than having:

Code
$hsh{k1}{k2}{k3}{k4}{k6}{k7}{k8} = "a thing";

you could have something like this:

Code
$hsh{"k1-k2-k3-k4-k6-k7-k8"} = "a thing";



alex5161
Novice

Nov 10, 2014, 8:41 AM

Post #28 of 30 (3108 views)
Re: [Laurent_R] perl: restrict perl from automaticaly creating a hash branches on check [In reply to] Can't Post


In Reply To
If the final interesting piece of data is always at the same level of nestedness, ... to have an auxilliary hash ...lower levels will autovivify, but you don't care, ...

The 'same level of nestedness' is not exactly true.
Better to say: existence concidered by reaching the hash highest level.
Actually, the keys are the data for most processing.
And, after all, all that true for my hashes that you are considered for that auxiliary hash.
(The idea to just disregard an empty branches and by that forget about autovivification at all already in my mind for couple days. Just did not have time yet to have code correction and test it...
But, what is bothering me is knowlage that something unnessesary is created, taking space and ... without my attention...
Whell, if all correction to just ignor empty branches (with disciplin to do not create any empty one in processing flow) would not destroy correctness, I guess, I will accept that autivivification addition as given and unavoidable trash (collaborated damage, if suitable :) )

Anyway, thanks for considering some effort on my task and for the suggestions!


alex5161
Novice

Nov 10, 2014, 8:51 AM

Post #29 of 30 (3106 views)
Re: [Laurent_R] perl: restrict perl from automaticaly creating a hash branches on check [In reply to] Can't Post


In Reply To
The other solution ... a flat hash ....

Code
$hsh{"k1-k2-k3-k4-k6-k7-k8"} = "a thing";


That is not suitable solution.
It would mean just to copy a file into internal memory and get copy of database table in processing.
I have already meat 'out of memory' retrieving DB information (resolved by 'slicing' the fetch.)
By having nested hash I have the information really shrinked more than twice. I have checked it.
Plus parcing such key on pieses ... well, maybe it is easy than handle hashes, but I am not sure... It could be harder (so, longer)
At least such way, for sure, will customize much more memory.


Laurent_R
Veteran / Moderator

Nov 10, 2014, 8:56 AM

Post #30 of 30 (3105 views)
Re: [alex5161] perl: restrict perl from automaticaly creating a hash branches on check [In reply to] Can't Post

You are welcome, Alex.

Yes, sometimes you don't really care about unwanted HoH autovivification, because you're still gonna get the correct answer (especially if you check that the value is defined and is not itself a hashref), sometimes you do because it does break your alrogithm, it really depends on what you are doing in your hash.

Otherwise, have you seen the other solution I proposed in post # 27 (i.e. a flat hash instead of a nested hash)? It might really be the best and fastest solution.

Update: I had not seen your latest post when I wrote the above.

(This post was edited by Laurent_R on Nov 10, 2014, 8:58 AM)

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives