Matching paired arrays into hash

Matching paired arrays into hash

am 15.08.2007 01:28:57 von 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[i]} = $arrayB[i].

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!

Re: Matching paired arrays into hash

am 15.08.2007 03:13:01 von Tad McClellan

kelticeye wrote:

> 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[i]} = $arrayB[i].


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

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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Matching paired arrays into hash

am 15.08.2007 03:22:34 von xhoster

kelticeye wrote:
> 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[i]} = $arrayB[i].

@hash{@arrayA}=@arrayB;

This is known as a hash slice.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: Matching paired arrays into hash

am 15.08.2007 03:36:59 von Charles DeRykus

On Aug 14, 4:28 pm, kelticeye wrote:
> 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[i]} = $arrayB[i].

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.



--
Charles DeRykus

Re: Matching paired arrays into hash

am 15.08.2007 09:46:31 von anno4000

kelticeye wrote in comp.lang.perl.misc:
> 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[i]} = $arrayB[i].

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

Re: Matching paired arrays into hash

am 15.08.2007 10:21:53 von kelticeye

Tad McClellan wrote:
> >See the "Slices" section in perldata, then use a hash slice:
> >
> > my %hash;
> > @hash{ @arrayA } = @arrayB;
>
> On Aug 15, 8:46 am, anno4...@radom.zrz.tu-berlin.de wrote:
> Applying the slice method to reproduce the result of your loop:
> On Aug 15, 8:46 am, anno4...@radom.zrz.tu-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