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.

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:

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. )

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

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)

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! )

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.