Matrix pairings to print 0 or 1 | Newbie | | Join Date: Jan 2008
Posts: 3
| |
U have one file
horse;tiger
lion;mule
lion;tiger
horse;mule
Another Second file
horse mule tiger lion
horse
mule
tiger
lion
Based on the first file that have pairings that is horse;tiger it will get 1 otherwise no parings will get 0
Output shud be like that
horse mule tiger lion
horse 0 1 1 0
mule 1 0 0 1
tiger 1 0 0 1
lion 0 1 1 0 -
use strict;
-
use warnings;
-
-
my %A;
-
my @order= sort keys %A;
-
-
while (<DATA>) {
-
warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/;
-
my ($row, $col) = ($1,$2);
-
$A {$row}{$col}++;
-
$A{$col}{$row}++;
-
-
-
print " ";
-
printf(" %s",$_) for (@order);
-
print "\n";
-
-
-
for my $row (@order) {
-
printf(" %s", $row);
-
-
for my $col (@order) {
-
printf(" %s", exists $A{$row}{$col} ? "1" : "0" );
-
}
-
print "\n";
-
}
-
-
-
__DATA__
-
horse;tiger
-
lion;mule
-
lion;tiger
-
horse;mule
-
-
But it is not getting why?
| |
best answer - posted by nithinpes |
Your script works absolutely fine if you put the order of animals in @order. Also, close the while loop after reading the data. -
use strict;
-
use warnings;
-
-
my %A;
-
my @order=qw(horse mule tiger lion);
-
-
while (<DATA>) {
-
warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/;
-
my ($row, $col) = ($1,$2);
-
$A {$row}{$col}++;
-
$A{$col}{$row}++;
-
}
-
-
print " ";
-
printf(" %s",$_) for (@order);
-
print "\n";
-
-
for my $row (@order) {
-
printf(" %s", $row);
-
-
for my $col (@order) {
-
printf(" %s", exists $A{$row}{$col} ? "1" : "0" );
-
}
-
print "\n";
-
}
-
-
-
__DATA__
-
horse;tiger
-
lion;mule
-
lion;tiger
-
horse;mule
-
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: Matrix pairings to print 0 or 1
Your script works absolutely fine if you put the order of animals in @order. Also, close the while loop after reading the data. -
use strict;
-
use warnings;
-
-
my %A;
-
my @order=qw(horse mule tiger lion);
-
-
while (<DATA>) {
-
warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/;
-
my ($row, $col) = ($1,$2);
-
$A {$row}{$col}++;
-
$A{$col}{$row}++;
-
}
-
-
print " ";
-
printf(" %s",$_) for (@order);
-
print "\n";
-
-
for my $row (@order) {
-
printf(" %s", $row);
-
-
for my $col (@order) {
-
printf(" %s", exists $A{$row}{$col} ? "1" : "0" );
-
}
-
print "\n";
-
}
-
-
-
__DATA__
-
horse;tiger
-
lion;mule
-
lion;tiger
-
horse;mule
-
| | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|