perl question

Michael Wittman plug-discuss@lists.plug.phoenix.az.us
Wed, 8 May 2002 22:36:19 -0700


On Wed, May 08, 2002 at 11:35:55PM -0500, Mike wrote:
> I am working on a script that takes a email message
> that is piped to it from a dot-qmail file. After reading
> through the file, I have
> created a hash of which the values are to be inserted
> into a MySQL db. Problem is, the keys do not coorespond
> to the field names in the table. 
> 
> Could someone help me run through this hash and "remap"
> the keys to a differant value?
> 
> For instance:
> 
> First_Name => Joe
> Last_name => Smith
> 
> needs to become
> 
> fname => Joe
> lname => Smith
> 
> v/r
> Mike

Try this:

my %hash_map = { First_Name => "fname", Last_name => "lname" }; # etc.
my %new_hash = map $hash_map{$_} => $old_hash{$_}, keys %old_hash;

-Mike