How to assign a Hash - newbie

E

evillen

Hi

Is there a more elegant to assign one hash to another - the solution
below works:

my $table = 'c';
my %dbitems;

if($table eq 'a'){%dbitems = %a}
elsif($table eq 'b'){%dbitems = %b}
elsif($table eq 'c'){%dbitems = %c}
elsif($table eq 'd'){%dbitems = %d}
elsif($table eq 'e'){%dbitems = %e}
elsif($table eq 'f'){%dbitems = %f}
elsif($table eq 'g'){%dbitems = %g}

but I hoped something like this would achieve the same:

my $table = 'c';
my %dbitems = %.$table;

I've tried variations but I get syntax errors such as:
"Can't use string ("c") as a HASH ref while "strict refs"" or "Only
hard references are allowed by "strict refs""

Thanks for any help
NJH
 
A

anno4000

Hi

Is there a more elegant to assign one hash to another - the solution
below works:

my $table = 'c';
my %dbitems;

if($table eq 'a'){%dbitems = %a}
elsif($table eq 'b'){%dbitems = %b}
elsif($table eq 'c'){%dbitems = %c}
elsif($table eq 'd'){%dbitems = %d}
elsif($table eq 'e'){%dbitems = %e}
elsif($table eq 'f'){%dbitems = %f}
elsif($table eq 'g'){%dbitems = %g}

but I hoped something like this would achieve the same:

my $table = 'c';
my %dbitems = %.$table;

You need an auxiliary hash (%sel below) to do that in a sane manner.
Untested:

my %sel;
@sel{ 'a' .. 'g'} = \ ( %a, %b, %c, %d, %e, %f, %g);

my $table = 'c';
my %dbitems = %{ $sel{ $table} };

You may be able to work with the hashrefs from %sel directly, without
copying the hash to %dbitems:

my $dbitems = $sel{ $table};

....and then use "$dbitems->{ $key}" instead of "$dbitems{ $key}".

Anno
 
M

Mumia W.

Hi

Is there a more elegant to assign one hash to another - the solution
below works:

my $table = 'c';
my %dbitems;

if($table eq 'a'){%dbitems = %a}
elsif($table eq 'b'){%dbitems = %b}
elsif($table eq 'c'){%dbitems = %c}
elsif($table eq 'd'){%dbitems = %d}
elsif($table eq 'e'){%dbitems = %e}
elsif($table eq 'f'){%dbitems = %f}
elsif($table eq 'g'){%dbitems = %g}

but I hoped something like this would achieve the same:

my $table = 'c';
my %dbitems = %.$table;

I've tried variations but I get syntax errors such as:
"Can't use string ("c") as a HASH ref while "strict refs"" or "Only
hard references are allowed by "strict refs""

Thanks for any help
NJH

Go into a command prompt and type

perldoc -q "variable as"

The Perl documentation on your system should explain this.

When you're done reading that, type "perldoc perlfaq"
 
M

mattsteel

(e-mail address removed) ha scritto:
Hi

Is there a more elegant to assign one hash to another - the solution
below works:

my $table = 'c';
my %dbitems;

if($table eq 'a'){%dbitems = %a}
elsif($table eq 'b'){%dbitems = %b}
elsif($table eq 'c'){%dbitems = %c}
elsif($table eq 'd'){%dbitems = %d}
elsif($table eq 'e'){%dbitems = %e}
elsif($table eq 'f'){%dbitems = %f}
elsif($table eq 'g'){%dbitems = %g}

but I hoped something like this would achieve the same:

my $table = 'c';
my %dbitems = %.$table;

I've tried variations but I get syntax errors such as:
"Can't use string ("c") as a HASH ref while "strict refs"" or "Only
hard references are allowed by "strict refs""

Thanks for any help
NJH

Maybe this is what you want.

my $table = 'c';
my %dbitems;
eval "\%dbitems = \%$table";

M.
 
E

evillen

You need an auxiliary hash (%sel below) to do that in a sane manner.
Untested:

my %sel;
@sel{ 'a' .. 'g'} = \ ( %a, %b, %c, %d, %e, %f, %g);

my $table = 'c';
my %dbitems = %{ $sel{ $table} };

You may be able to work with the hashrefs from %sel directly, without
copying the hash to %dbitems:

my $dbitems = $sel{ $table};

...and then use "$dbitems->{ $key}" instead of "$dbitems{ $key}".

Anno


Thanks guys, however I was advised to try using a HOH - so now I am
trying this code:


#START
#!c:/perl/bin/perl.exe -w

use strict;
use diagnostics;

#-------------------------------------------------
#Description of hashes is: fieldname[label,length,category,tick,order]
my %HoH = (
names => {
fore => ['Frank','Alex','Wayne'],
sur => ['Lampard','Ferguson','Rooney'],
},
places => {
north => ['Burnley', 'Chester', 'Doncaster'],
south => ['Brighton', 'Cheltenham', 'Dover'],
},
);

my $table = "names";
my %Hash = $HoH{$table};

print "My \%Hash is: ", %Hash;

#END

however this won't assign a Hash to Hash as expected. How do I assign
the name/value pairs of "names" to %Hash?

NJH
 
M

mattsteel

my $table = "names";
my %Hash = $HoH{$table};

print "My \%Hash is: ", %Hash;

#END

however this won't assign a Hash to Hash as expected. How do I assign
the name/value pairs of "names" to %Hash?

NJH

my %Hash = %{$HoH{$table}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top