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

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Populating and accessing complicate Hashes
by bobf (Monsignor) on Apr 24, 2006 at 04:37 UTC

    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.

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

Re: Populating and accessing complicate Hashes
by GrandFather (Saint) on Apr 24, 2006 at 04:05 UTC

    The sample data would help. I recommend that you use a __DATA__ section and replace <CDF> with <DATA> for the purposes of this post.

    Note that you really should use strict; use warnings; in all code you write!

    Where does %basic_references come from? Where is %main_hash declared?

    Update: more grumbling


    DWIM is Perl's answer to Gödel
    A reply falls below the community's threshold of quality. You may see it by logging in.