in reply to Array comparison for 3 arrays

Essentially you want to transform the original data structure into something that can be easily iterated and displayed, in the way you wanted.

use strict; use warnings; use Data::Dumper; my $arrays = {"a1" => [0, 1,2,3,4,5,6,7,8,9], "a2" => [1,2,3,4,6,8, 10, 12,14], "a3" => [1,2,3,5,7,9, 11,13,15]}; my $result = {}; foreach my $array_name (keys %$arrays) { map {$result->{$_}->{$array_name} = 1} @{$arrays->{$array_name}} } print Dumper($result);

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^2: Array comparison for 3 arrays
by GrandFather (Saint) on Apr 14, 2010 at 06:54 UTC

    Do not use map in void context. Although it probably doesn't generate the output list in current versions of Perl, it does send wrong signals about the intent of the code. Use for as a statement modifier instead:

    $result->{$_}{$array_name} = 1 for @{$arrays->{$array_name}}
    True laziness is hard work

      Don't use postfix for. :)

      my @array = $arrays->{$array_name}; for my $number (@array) { $result->{$number}->{$array_name} = 1 }

        There is no need to be excessively verbose when expounding on the subject at hand. It helps if you get the syntax right in your example code too.

        for my $number (@{$arrays->{$array_name}}) { $result->{$number}{$array_name} = 1; }

        The @{...} is required to dereference the array reference which is the hash value. Without the dereference syntax @array is assigned (or the loop iterates over) a single element which is the reference to the array.

        True laziness is hard work