Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!perl
- =head http://blogs.perl.org/users/ovid/2014/12/a-small-puzzle-for-you.html
- Given several arrays, each of which has elements which are a subset of allowed elements,
- and given that every allowed element appears at least once in each array,
- how do I rewrite all arrays such that each element of each array which has an identical value to an element
- in another array has the same index number in each array, with missing elements being undef?
- =cut
- @a = ( 'b', 'c', 'f' );
- @b = ( 'a', 'd' );
- @c = ( 'c', 'd', 'e' );
- my @arrs = (\@a, \@b, \@c);
- my %h;
- $h{$_}++ for map{ +(@$_) } @arrs;
- my @list = sort keys %h;
- my @new_arrs;
- for( my $i = 0; $i < @list; $i++ ){
- for(my $a = 0; $a < @arrs; $a++){
- my $val = $list[$i];
- $val = undef unless( grep {$list[$i] eq $_ } @{$arrs[$a]} );
- push @{$new_arrs[$a]}, $val;
- }
- }
- use Data::Dumper;
- $Data::Dumper::Indent = 1;
- print Dumper \@new_arrs;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement