in reply to making presence/absence table from a hash of arrays

$ 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

Replies are listed 'Best First'.
Re^2: making presence/absence table from a hash of arrays
by Anonymous Monk on Sep 06, 2011 at 09:04 UTC

    The grep can be simplified with a smart match:

    for my $key (sort keys %hash) { say "$key = ", join ' ', map $_ ~~ $hash{$key} ? 1 : 0, @array; }