Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Help on Hashes

by Anonymous Monk
on May 14, 2014 at 07:24 UTC ( #1085984=perlquestion: print w/replies, xml ) Need Help??

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

Hi Experts,

I am storing some database column names in hash and based on the parameter passed with perl script i need to selecr the particular hash

Example:

test.pl A, the hash to be selected is @COL_CHECK_A and if its B, we need to select @COL_CHECK_B

my @hash; my @field; my @COL_CHECK_A = qw( NAME CITY STATE ); my @COL_CHECK_B = qw( NAME CITY STATE COUNTY ); my @COL_CHECK_C = qw( NAME AGE STATE COUNTY ); my @COL_CHECK_D = qw( NAME AGE CITY STATE COUNTY ); my @COL_CHECK_E = qw( NAME AGE DESIGNATION CITY STATE COUNTY );
foreach my $key (@COL_CHECK_$ARGV[0]) { push( @field, $hash{$key} ); }

Any suggestions please.

Replies are listed 'Best First'.
Re: Help on Hashes
by hdb (Monsignor) on May 14, 2014 at 07:35 UTC

    It is better to store your data in a hash of array (references) like this:

    use strict; use warnings; my %COL_CHECK = ( A => [ qw( NAME CITY STATE ) ], B => [ qw( NAME CITY STATE COUNTY ) ], C => [ qw( NAME AGE STATE COUNTY ) ], D => [ qw( NAME AGE CITY STATE COUNTY ) ], E => [ qw( NAME AGE DESIGNATION CITY STATE COUNTY ) ] ); for my $key (@{$COL_CHECK{$ARGV[0]}}) { print "$key\n"; }
Re: Help on Hashes
by muba (Priest) on May 15, 2014 at 15:33 UTC

    You seem to have confused arrays and hashes. Arrays don't have keys.(edit: irrelevant, I misread your code).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-04-19 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?