CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Assign values to multiple hash keys from array

 



rpaskudniak
User


Aug 28, 2013, 8:18 AM

Post #1 of 7 (1592 views)
Assign values to multiple hash keys from array Can't Post

Hi folks.

After some of the things I've done in Perl I'm almost embarrassed to be posting on the beginner's forum. Good thing I'm hiding behind the scary face. Angelic

In many situations e.g. a database FETCH that returns an array of column values, it would be convenient to assign multiple elements in a hash to the values in the array, all in one statement. I thought I had seen an example of that in a book but I can't find it again. Here's a simple example of what I'm looking for:

Code
my %myhash; 

@{$myhash{{A}, {B}, {C}, {D}} = qw(first second third fourth);

Of course, this gets a syntax error.

I also tried this:

Code
my @vals = qw(first second third fourth); 
@{$myhash{qw(A B C D)}} = @vals;

but Dumper([%myhash]) shows:

Code
$VAR1 = [ 
'ABCD',
[
'first',
'second',
'third',
'fourth'
]
];

so it combined the qw'ed elements to be the key whose value is the array.

What I want is to assign:
$myhash{A} = $vals[0]
$myhash{B} = $vals[1]
$myhash{C} = $vals[2]
$myhash{D} = $vals[3]
all in one single statement, not in a loop. I also want to avoid the cumbersome:

Code
($myhash{A}, $myhash{B}, $myhash{C}, $myhash{D}) = @vals;

even though that works. I don't want to have to repeat the hash name each time because that's error prone when dealing with a database app with many record structures. (And it's cumbersome enough when the hash keys are really the names of database columns. Crazy )

This comes down to referencing multiple hash elements in a list.

How do I do this?

Thanks much!
--------------------
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


FishMonger
Veteran / Moderator

Aug 28, 2013, 8:44 AM

Post #2 of 7 (1590 views)
Re: [rpaskudniak] Assign values to multiple hash keys from array [In reply to] Can't Post

What you're wanting is called a hash slice.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @keys = qw(A B C D);
my @values = qw(first second third fourth);

my %hash;
@hash{ @keys } = @values;
print Dumper \%hash;


perldoc perldata - http://perldoc.perl.org/perldata.html#Slices


rpaskudniak
User


Aug 28, 2013, 3:01 PM

Post #3 of 7 (1583 views)
Re: [FishMonger] Assign values to multiple hash keys from array [In reply to] Can't Post

Ah, Fishmonger to the rescue! How can I ever thank you?! Blush

OK, the code I am test-driving now looks like:

Code
 @myhash{qw(A B C D)} = qw(first second third fourth);

This is more akin to the way I would use it in a database fetch:

Code
@cust_rec{qw(name address phone order_id order_cost)} 
= $cust_cursor->fetch();

Although for a very long list of columns (for those of us who must deal with data warehouse fact tables Unsure and Frown ) the separate array of member names (usually corresponding to names of database columns) is the better solution.

Kudos!
--------------------
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)
Attachments: kudos.jpg (26.8 KB)


FishMonger
Veteran / Moderator

Aug 28, 2013, 4:50 PM

Post #4 of 7 (1577 views)
Re: [rpaskudniak] Assign values to multiple hash keys from array [In reply to] Can't Post

You do know, don't you, that you could fetch a hash ref directly where its keys are the field/column names?

Or, you could use

Code
my $fields = $sth->{NAME};

to get an array ref of the names.


rpaskudniak
User


Aug 29, 2013, 4:52 AM

Post #5 of 7 (1572 views)
Re: [FishMonger] Assign values to multiple hash keys from array [In reply to] Can't Post


Quote
You do know, don't you, that you could fetch a hash ref directly where its keys are the field/column names?

Actually, I didn't know. Would you please provide an example of such a fetch? I'm guessing the field names must already be defined before I try to fetch this way. I'm quite open to correction,

I will confess to never having done anything more sophisticated with DBI than a simple fetch into an array. If the explanation is too long, please just refer me to the right place. (I do have a hard-copy of "Using the Perl DBI".)

Thank you for eye-opener as well as the syntax I originally asked about.
--------------------
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


(This post was edited by rpaskudniak on Aug 29, 2013, 4:56 AM)


FishMonger
Veteran / Moderator

Aug 29, 2013, 6:18 AM

Post #6 of 7 (1568 views)
Re: [rpaskudniak] Assign values to multiple hash keys from array [In reply to] Can't Post

Have you read the DBI documentation? It explains how to fetch row by row putting it into a hash ref and how to fetch all rows at once using a hash ref.

http://search.cpan.org/~timb/DBI-1.628/DBI.pm#fetchrow_hashref


rpaskudniak
User


Aug 29, 2013, 2:23 PM

Post #7 of 7 (1559 views)
Re: [FishMonger] Assign values to multiple hash keys from array [In reply to] Can't Post

Fishmonger,

Yes I have read it, though perhaps not as thoroughly as if I were actually on a DBI project at the moment. And I see in all cases it seems to return an array, which is just fine. I misunderstood you initially and thought you meant that I could somehow fetch into a hash and have all the column names (that I would get from $sth->fetchrow_hashref($name);) assigned as members of the hash and would get the appropriate values. I see from this and your remarks that I could do this in two statements: Get the array of column names, then fetch the array of column values into @target_hash{@column_name_array}.

(If you can do it on 1 statement, I don't want to know about it! Crazy )

In any case, this thread has skidded off the initial topic, the hash slice, which you have answered with your usual aplomb and I am grateful for that. The DBI stuff I can shelve for now.

Wait till you see my next question, in intermediate questions. Pirate (Hmm.. No devil icon.. :)
--------------------
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives