hashtable - Efficiently get hash entry only if it exists in Perl -
I am often writing pieces of code like this:
if ($ myHash Exists - & gt; {$ key}) {$ value = $ myHash-> {$ Key}; } What I am trying to do is get value from hash if that has the key in hash, and at the same time I want to avoid having to turn on hash entry automatically Already exists.
Although it kills me that it is quite incompetent: I am searching a hash to know that a key is present, and then if it exists then I have another hash key to remove it.
This becomes even more inefficient in a multilevel structure:
if ($ myHash-> $$ exists-> $ {key1} & amp; & Amp; amp; amp; amp; amp; MyHash- & gt; {$ key1} {$ key2} & amp; amp; is available $ myHash- & gt; {$ key1} {$ key2} {$ key3 }) {$ Value = $ myHash-> {$ Key1} {$ Key2} {$ key3}; } Here I'm doing 9 hash lookup instead of 3!
Is Pearl enough to adapt such a case? Or is there any other phrase to get the value of an hash to automatically verify the entry or to see it twice?
I have information about modules, but if possible, I am looking for a solution that requires an XS module to install, besides I did not get the chance to exclude this module And I'm not completely sure what happens in the case of multilevel hash - the crop says that this:
$ h-> gt; {$ Key} The key is not present if it does not return - this means that:
$ h-> {$ Key1} {$ Key2} If $ key1 was not present, then died on the basis that I'm trying to undermine the D-referee? If so, then you will probably need to do multi-level testing for survival. Do not worry about optimization because hash lookup is fast but for your first case, you can:
if (my $ v = $ hash {$ key}) { Print "$ key = & gt; $ v \ n"; } Similarly:
if (($ v = $ hash {key1}) & amp; ($ v = $ v- & Gt; {Key2})) {Print "Got $ V \ n"; }
Comments
Post a Comment