#!/ichigo/perl use v5.12; use warnings; use strict; 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 / ], ); say "@array"; # Get index mapping. my %index; @index{@array} = 0..$#array; my @zero = (0) x @array; my @one = (1) x @array; for my $key (sort keys %hash) { # Zero-out a bit array. my @bits = @zero; # Flip bits to one using index. @bits[@index{@{$hash{$key}}}] = @one; say "$key = @bits"; }