Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Anonymous Monk
on Sep 06, 2011 at 22:08 UTC ( #924473=note: print w/ replies, xml ) Need Help??


in reply to Re^4: making presence/absence table from a hash of arrays
in thread making presence/absence table from a hash of arrays

The innermost foreach loop can also be replaced with array/hash slices:

#!/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"; }


Comment on Re^5: making presence/absence table from a hash of arrays
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-07 23:09 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 (93 votes), past polls