Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Populating and accessing complicate Hashes

by bobf (Monsignor)
on Apr 24, 2006 at 04:37 UTC ( #545205=note: print w/replies, xml ) Need Help??

in reply to Populating and accessing complicate Hashes

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

  • minimise in accesing this hash and creating this hash
  • whethere my code is sufficient or not
  • improve the performance

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.



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];

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://545205]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-05-23 08:13 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (81 votes). Check out past polls.