- You need to find the value at which to start.
- You need to find the value at which to stop.
- You need some way of determining if a value is present in an array. (I used a hash rather than grepping the entire array over and over again.)
use strict;
use warnings;
my @arrays = (
[0,1,2,3,4,5,6,7,8,9],
[1,2,3,4,6,8,10,12,14],
[1,2,3,5,7,9,11,13,15],
);
my $min = my $max = $arrays[0][0];
my @lkups;
for (0..$#arrays) {
my $array = $arrays[$_];
my $lkup = \%{ $lkups[$_] };
for (@$array) {
$min = $_ if $_ < $min;
$max = $_ if $_ > $max;
++$lkup->{$_};
}
}
for my $i ($min..$max) {
print(join("\t", $i, map { $_->{$i} ? 'yes' : '' } @lkups), "\n");
}
Alternatively, you could take advantage of the fact that the numbers are sorted and do something like a merge sort.
Update: Oops, I thought you wanted continuous indexes, but I doubt that now. Fix:
- You need to find which values are present in any array.
- You need some way of determining if a value is present in an array. (I used a hash rather than grepping the entire array over and over again.)
use strict;
use warnings;
my @arrays = (
[0,1,2,3,4,5,6,7,8,9],
[1,2,3,4,6,8,10,12,14],
[1,2,3,5,7,9,11,13,15],
);
my %global_lkup;
my @lkups;
for (0..$#arrays) {
my $array = $arrays[$_];
my $lkup = \%{ $lkups[$_] };
for (@$array) {
++$lkup->{$_};
++$global_lkup{$_};
}
}
for my $i ( sort { $a <=> $b } keys(%global_lkup) ) {
print(join("\t", $i, map { $_->{$i} ? 'yes' : '' } @lkups), "\n");
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.