perl - Compare two hash of arrays -
i have 2 arrays , hash holds these arrays
array 1: $group = "west" @{ $my_big_hash{$group} } = (1534,2341,2322,3345,689,3333,4444,5533,3334,5666,6676,3435); array 2 : $element = "location" ; $group = "west" ; @{ $my_tiny_hash{$element}{$group} } = (153,333,667,343);
now want compare
@{ $my_tiny_hash{$element}{$group} }
with
@{ $my_big_hash{$group} }
and check whether elements of tiny hash array part of big_hash array . can see tiny hash has 3 digit elements , these elements matching big hash if compare first 3 digits
if first 3 digits/letters match , available in big array, matching or have print unmatched elements
its array array comparison. how achieve it.
ps : without array utils , how achieve it
the solution using array utils simple
my @minus = array_minus( @{ $my_tiny_hash{$element}{$group} } , @{ $my_big_hash{$group} } );
but compares digits , want match first 3 digits
hope clear
thanks
this seems want.
#!/usr/bin/perl use strict; use warnings; use 5.010; (%big_hash, %tiny_hash); $group = 'west'; $element = 'location'; # less confusing initialisation! $big_hash{$group} = [1534,2341,2322,3345,689,3333,4444,5533,3334,5666,6676,3435]; $tiny_hash{$element}{$group} = [153,333,667,343]; # create hash keys first 3 digits of numbers # in big array. doesn't matter values are. %check_hash = map { substr($_, 0, 3) => 1 } @{ $big_hash{$group} }; # grep small array checking elements' existence in %check_hash @missing = grep { ! exists $check_hash{$_} } @{ $tiny_hash{$element}{$group} }; "missing items: @missing";
update: solution seems closer original code.
my @truncated_big_array = map { substr($_, 0, 3) } @{ $big_hash{$group} }; @minus = array_minus( @{ $my_tiny_hash{$element}{$group} } , @truncated_big_array );
Comments
Post a Comment