Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Missing something with regards to references

by dpuu (Chaplain)
on Nov 01, 2004 at 22:42 UTC ( #404443=note: print w/replies, xml ) Need Help??


in reply to Missing something with regards to references

Sometimes with these "what am I missing" type of questions, the best approach is to explain how I'd do it, and then see if that answer somehow hits the one thing you need to understand.

Lets start with the function usage model: You suggest

put_element( $DATA{3}, 'h', 'i', 'worked' );
For clarity, lets do the whole hash. You can create a reference to the top level (root) hash using the backslash operator:
put_element( \%DATA, 3, 'h', 'i', 'worked' );
All those quotes get tiresome after a while, so lets get rid of them:
put_element( \%DATA, qw(3 h i worked) );
Perl flattens arg-lists, so that is identical to the previous version.

So now the implementation (I'll omit the error-checks):

sub put_element { my ($hashref, @path) = @_; # The leaf node and its value are special my $value = pop @path; my $leaf = pop @path; # walk the tree, create non-existing nodes for my $node (@path) { $hashref = $hashref->{$node} ||= {}; } # Finally, set the value at the leaf $hashref->{$leaf} = $value; }
Hopefully the comments are sufficient to explain my thinking.

--Dave
Opinions my own; statements of fact may be in error.

Replies are listed 'Best First'.
Re^2: Missing something with regards to references
by scottb (Scribe) on Nov 02, 2004 at 00:03 UTC
    This is very elegant and makes much more sense to me, so thank you dpuu. But given some of the points that blokhead raised, are there any other gotchas with this approach I should be concerned about?
      I don't think so: my explicit default-assignment should avoid any autovivication issues.
      --Dave
      Opinions my own; statements of fact may be in error.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://404443]
help
Chatterbox?
[Lady_Aleena]: Discipulus, it is at 2 now, but with what I am thinking about, it could go to 3. However, only 1 is needed. The second and third are optional.
[shmem]: straight list or named parameters - that depends on whether (and how many) optional arguments you have
[Discipulus]: if so a plain list is ok, imho
[Lady_Aleena]: shmem, I already have the plugin installed, just not active.
[shmem]: if you have 1 optional argument, place that at the end of the list. If you have more, go for named parameters.
[Lady_Aleena]: 1 manditory, 2 optional.
[Discipulus]: my ($need,$opta,$optb ) = @_; .. if $opta..
[shmem]: all else leads to trouble, even if the third argument depends on the existence of the second. That may become brittle.
[Discipulus]: but if have case like subname(15,undef,3 ) maybe bettere named parameters
[Lady_Aleena]: I don't want to have to do: alpha_menu($hash, undef, $type);

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2017-04-27 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.