2

(Regarding the duplication: the pointed discussion is related, but different in target: that author looking for a way to find all places of such occurrences, while I know all of them, but looking for a way to avoid such behavior! Therefore, while it is educational to read that 'duplication', this question is different!)

My issue is that the perl script (as I have done it so far) creates empty branches when I try to check some branches on existence.

I am using multidimensional hashes: found it as the best way for information that I need to handle. Saying multidimensional I means hash of hashes ... So, I have

$hsh{k1}{k2}{k3}{k4}=v1;
$hsh{k1}{k9}{k3}{k33}=v2;
....

In some point I need to check some 'set of keys' on existence in the hash. So, I check:

if ( $hsh{ch1}{ch2}{ch3} ) {  ...something... }

And, suddenly I've realized, that such check creates the intermediate branches to access the final one for checking!

So, if there is no $hsh{ch1} before the check, it will be created to access the key 'ch2', which will be created to access the 'ch3'

QUESTION: Is there a way to avoid such aromatic creation ????

Logically, I would expect the Perl to get FALSE as soon as any key is not exist in the hash, but it is not happening!

Sure I could do:

if ( $hsh{ch1} && $hsh{ch1}{ch2} && $hsh{ch1}{ch2}{ch3} ) 
{...}

but it is so annoying, especially when I have many hashes with 7- 10 keys!

I have creates some function to check a hash 'reasonably', sending hash reference and array of keys to be checked, and it works fine, but it is slows down the processing visibly.

Here is an example of what I am talking about:

  DB<4> $h{a}{b}=[1,2]; $h{a}{c}=[3,4]  #hash with two elements (arrays) and in 2 levels

  DB<5> use Data::Dumper  #good way to display 

  DB<6> p Dumper\($a,%h)
$VAR1 = \undef;
$VAR2 = {
          'a' => {
                   'c' => [
                            3,
                            4
                          ],
                   'b' => [
                            1,
                            2
                          ]
                 }
        };

  DB<7> p 'da' if !$h{p}{m}  #checking for an existent keys and element
da
  DB<8> p Dumper\($a,%h)
$VAR1 = \undef;
$VAR2 = {
          'p' => {},            # <<== created one !!!
          'a' => {
                   'c' => [
                            3,
                            4
                          ],
                   'b' => [
                            1,
                            2
                          ]
                 }
        };

  DB<9> delete $h{p}

Once again: Is there a way to avoid such behavior without creating additional function?

arhak
  • 2,488
  • 1
  • 24
  • 38
alex5161
  • 19
  • 5

2 Answers2

3

Use the autovivification module from CPAN.

arhak
  • 2,488
  • 1
  • 24
  • 38
nickcrabtree
  • 358
  • 1
  • 10
  • Thanks, it would be good, if I could install any CPAN: not my system... --- I have some replies on 'Perl Guru' forum [(Perl-Guru-my-post)](http://perlguru.com/gforum.cgi?post=80149;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread) and, I believe, there is nothing more need to be discussed on that issue. --- Thank, anyway! – alex5161 Nov 06 '14 at 20:38
0
use autovivification;

no autovivificaiton; # disable perl's autovivification feature from this point

if ( $hsh{ch1}{ch2}{ch3} ) {  ...something... }

see autovivification module

arhak
  • 2,488
  • 1
  • 24
  • 38