http://qs1969.pair.com?node_id=545205


in reply to Populating and accessing complicate Hashes

I'm afraid I don't know what you mean when you say:

For example, you can minimize accessing and creating the hash by removing the code that does those things, but I don't think that's what you mean. Sufficiency is dictated at least in part by whether or not the code meets your requirements (does it?). Re: performance, is there a specific bottleneck in your code that you're trying to refactor (i.e., have you benchmarked it?) or are you looking for general optimizations?

If you could clarify your OP, we will be able to give you much more specific answers.

For starters, though, you might want to consider cleaning up the innermost guts of the nested foreach loops by taking advantage of Perl's reference syntax (see perlref, perldsc, and perlreftut).

For example, instead of using constructs like ${$main_hash{$val}{$type}{$pegs}{'Section1'}}[0] everywhere, IMO it is cleaner and possibly more efficient to add a temp variable for the innermost reference:

my $href = $main_hash{$val}{$type}{$pegs}; my $T1val = defined $href->{'Section1'}[0] ? $href->{'Section1'}[0] : +undef;
This will reduce the code density of the inner loop, leaving less room for typos (use strict! use warnings!) and making the code easier to read/understand/maintain.

HTH

Update:

I'm confused. Why do you do this:

$T1val = defined ${$main_hash{$val}{$type}{$pegs}{'Section1'}}[0] ? ${ +$main_hash{$val}{$type}{$pegs}{'Section1'}}[0] : undef;

If the value is defined you assign that value to $T1val. If it is undefined you assign undef, but that is no different than what would have happened if you skipped the defined test and ternary operator. In other words, the whole line above is equivalent to this:

$T1val = ${$main_hash{$val}{$type}{$pegs}{'Section1'}}[0];