Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: making presence/absence table from a hash of arrays

by jwkrahn (Monsignor)
on Sep 05, 2011 at 19:58 UTC ( #924291=note: print w/ replies, xml ) Need Help??


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


Comment on Re: making presence/absence table from a hash of arrays
Download Code
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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924291]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (16)
As of 2015-07-07 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (89 votes), past polls