$ perl -le' my @array = qw/ one two three four five /; my %hash = ( row_1 => [ qw/ one five two / ], row_2 => [ qw/ four two / ], row_3 => [ qw/ three one five four / ], ); print "@array"; for my $key ( sort keys %hash ) { print "$key = ", join " ", map { my $elem = $_; grep( $_ eq $elem, @{ $hash{ $key } } ) ? 1 : 0 } @array; } ' one two three four five row_1 = 1 1 0 0 1 row_2 = 0 1 0 1 0 row_3 = 1 0 1 1 1