Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Creating a hash with the same name as the value of $hashref-{y}

by kingman (Scribe)
on Aug 15, 2001 at 22:56 UTC ( [id://105138]=perlquestion: print w/replies, xml ) Need Help??

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

I'm passing around a bunch of hash references to various subroutines and things are getting kind of ugly. I don't know if I need to use symbolic references of eval something or if there's any better way to do this. Please consider the following code:
#! /usr/bin/perl -w $db{x} = 'file'; $db{y} = 'name_of_hash'; $db{z} = 'key'; $hashref = \%db; print $hashref->{y}; # Is there a better way to create a new hash with the same # name as the value found in $hashref->{y} (which in this # case is 'name_of_hash'). I'm trying to automatically # create %name_of_hash. # This assigment seems to work but I'd hate to debug it six # months from now. ${$hashref->{y}}{$hashref->{z}} = 'value'; # But I can't print it print "${$hashref->{y}}{$hashref->{z}}\n"; # I have to do this. print "$name_of_hash{$hashref->{z}}\n"; # All this for this! print $name_of_hash{key}

Replies are listed 'Best First'.
Re: Creating a hash with the same name as the value of $hashref-{y}
by runrig (Abbot) on Aug 15, 2001 at 23:09 UTC
    Do you really need to do this? You would not be using strict and you would be using symbolic references if you actually do this. Can't you just create a master hash of hashes, and have the keys be the value your talking about?
    my %master; $master{$hashref->{y}}{$hashref->{z}} = 'value'; # In the spirit of giving 'em enough rope, # and hoping I never have to maintain the code, # here's the symbolic reference version ${$$hashref{y}}->{$$hashref{z}} = 'value';
      The trouble is the values are coming from user input so I can't create a hash of hashes :(
        The trouble is the values are coming from user input so I can't create a hash of hashes.

        How is that a valid reason? Please don't use symbolic references without a good reason. Processing user input is an ever better reason NOT to use symbolic references. What if you end up trashing a perlvar? Create a HoH and don't live dangerously, here's a snippet that processes input:

        my %HoH; while (<DATA>) { my ($name, $key, $value) = split; $HoH{$name}{$key} = $value; } __DATA__ name key value a b c 1 2 3
Re: Creating a hash with the same name as the value of $hashref-{y}
by blakem (Monsignor) on Aug 15, 2001 at 23:11 UTC
    Why don't you just use a hash of hashes?

    #! /usr/bin/perl -w %data = ('name_of_hash_a' => {key => 'value'}, 'name_of_hash_b' => {key => 'value2'}, ); for my $hashname ('name_of_hash_a','name_of_hash_b') { my $value = $data{$hashname}{key}; print "Hash:$hashname Key:key Value:$value\n"; }

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found