Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Using multi-level hashes

by shemp (Deacon)
on Oct 28, 2005 at 21:39 UTC ( [id://503782]=note: print w/replies, xml ) Need Help??


in reply to Using multi-level hashes

As for your first question of how to reduce the amount of typing, it very much depends on what you want to do with your structure. If you're populating it, you probably have some simpler structures that contain the data being entered into the 3-level hash. Loop through them. Or if you're manually adding each elemnt, you're somewhat stuck.
For the second part, in relation to your readItem() function, and assigning to the structure, you have multiple problems. First off, you should pass you structure in to the function, instead of using it as a global, i.e.:
... # populate your %project readItem(\%project); ... sub readItem { my ($project) = @_; my $sub_project = $project->{...}->{...}; etc. }
If that doesn't make sense, you need to read more about variable scope.
As for assigning into a referenced subpart, the important thing to consider is what just got deferenced when you created the sub part. Is it a hashref thats also pointed to by your big structure, or is it a scalar, or is it a newly created anonymous structure?
my %multi_level = ( 'foo' => { 1 => 'A', 2 => 'B' }, 'bar' => { 'C' => 'see', 'T' => 'tea' }, ); my $sub_part = $multi_level{foo}; # sub_part is a hashref, # also pointed at by $multi_level{foo} print $multi_level{foo}->{1} . "\n"; # works (prints 'A') $sub_part->{3} = 'C'; print $multi_level{foo}->{3} . "\n"; # works (prints 'C') my $part_two = $multi_level{foo}->{1}; # part_two is a # scalar, a copy of the value of $multi_level{foo}->{1}, # namely 'A'. Changing it will not change %multi_level
the $part_two example may make more sense if you think of this:
my $a = 'blah'; my $b = $a; $b = 'foo'; print "$a\n"; # prints 'blah' print "$b\n"; # prints 'foo'

I use the most powerful debugger available: print!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found