in reply to Re^3: how to use the values of the first hash to be the keys for the second hash
in thread how to use the values of the first hash to be the keys for the second hash

This is so great.

But the values in my hash1 are not arrays, they are strings with each key for hash2 separated by comma ",". How should I turn the strings in to array so that I could adapt to the code?

Thanks again, great help!

  • Comment on Re^4: how to use the values of the first hash to be the keys for the second hash

Replies are listed 'Best First'.
Re^5: how to use the values of the first hash to be the keys for the second hash
by choroba (Cardinal) on Sep 07, 2012 at 23:21 UTC
    Ah, ok, that was originally not clear from the question. You can use split to get the list from the string:
    #!/usr/bin/perl use warnings; use strict; my %hash1 = (key1 => 'val1,val2', key2 => 'val3', ); my %hash2 = (val1 => 'val5,val6,val7', val2 => 'val8,val9', val3 => 'val3', ); my %result = map { $_ => join ',', map split(/,/, $hash2{$_}), split /,/, $hash1{$_} } keys %hash1; while (my ($k, $v) = each %result) { print "$k: $v\n"; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ