Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Populating and accessing complicate Hashes

by jesuashok (Curate)
on Apr 24, 2006 at 03:59 UTC ( [id://545193]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://545193]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found