Matching paired arrays into hash

K

kelticeye

Hi, this is my first post to this group.

I was wondering if there is an efficient method in perl for joining
two (properly ordered) arrays of equal length (@arrayA and @arrayB)
into a hash(%hash) so that $hash{$arrayA} = $arrayB.

I need to do this as the first list is part of an incremental query to
a webform, and if the response is as expected (split up with a regex),
this should be proportional to the query and ordered in the same way
as the query.

Presently I do this through a loop, however this is ugly, creating
iterating variables and I feel I am missing a trick (BTW this does
work):-

my %HresHash;
if (scalar(@residuelist) == scalar(@fullH)){
for (my $i = 0; $i < scalar(@fullH) ; $i++){
my $reskey = "res".$fullH[$i]; # this is done as I want
to add it to a global hash later
$HresHash{$reskey}= $residuelist[$i];
}
}

I have looked through the faqs, this group and google for an easy
answer or function, but please let me know if I am wasting your time
as I will appreciate being pointed in the right direction!
 
T

Tad McClellan

kelticeye said:
I was wondering if there is an efficient method in perl for joining
two (properly ordered) arrays of equal length (@arrayA and @arrayB)
into a hash(%hash) so that $hash{$arrayA} = $arrayB.



See the "Slices" section in perldata, then use a hash slice:

my %hash;
@hash{ @arrayA } = @arrayB;
 
X

xhoster

kelticeye said:
Hi, this is my first post to this group.

I was wondering if there is an efficient method in perl for joining
two (properly ordered) arrays of equal length (@arrayA and @arrayB)
into a hash(%hash) so that $hash{$arrayA} = $arrayB.


@hash{@arrayA}=@arrayB;

This is known as a hash slice.

Xho
 
C

comp.llang.perl.moderated

Hi, this is my first post to this group.

I was wondering if there is an efficient method in perl for joining
two (properly ordered) arrays of equal length (@arrayA and @arrayB)
into a hash(%hash) so that $hash{$arrayA} = $arrayB.


use List::MoreUtils qw/mesh/;

my %hash = mesh @arrayA, @arrayB;
I need to do this as the first list is part of an incremental query to
a webform, and if the response is as expected (split up with a regex),
this should be proportional to the query and ordered in the same way
as the query.

Presently I do this through a loop, however this is ugly, creating
iterating variables and I feel I am missing a trick (BTW this does
work):-

my %HresHash;
if (scalar(@residuelist) == scalar(@fullH)){
for (my $i = 0; $i < scalar(@fullH) ; $i++){
my $reskey = "res".$fullH[$i]; # this is done as I want
to add it to a global hash later
$HresHash{$reskey}= $residuelist[$i];
}
}

Not sure how this follow-on example relates to
your earlier problem statement.
 
A

anno4000

kelticeye said:
Hi, this is my first post to this group.

I was wondering if there is an efficient method in perl for joining
two (properly ordered) arrays of equal length (@arrayA and @arrayB)
into a hash(%hash) so that $hash{$arrayA} = $arrayB.


Xho and Tad have shown the basic method of using a hash slice to do
this.

[...]
my %HresHash;
if (scalar(@residuelist) == scalar(@fullH)){
for (my $i = 0; $i < scalar(@fullH) ; $i++){
my $reskey = "res".$fullH[$i]; # this is done as I want
to add it to a global hash later
$HresHash{$reskey}= $residuelist[$i];
}
}

Applying the slice method to reproduce the result of your loop:

my %HresHash;
@HresHash{ map "res$_", @fullH} = @residuelist;

Anno
 
K

kelticeye

Tad McClellan said:
On Aug 15, 8:46 am, (e-mail address removed)-berlin.de wrote:
Applying the slice method to reproduce the result of your loop:
On Aug 15, 8:46 am, (e-mail address removed)-berlin.de wrote:
my %HresHash;
@HresHash{ map "res$_", @fullH} = @residuelist;

Anno

Thank you all for your excellent response to my post and making me
aware of the concise power of hash slices.

Thank you, particularly to Anno for the tailored solution for my code.
I imagine this being very useful to know in future.

Regards,
Brian
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top