Connecting Tech Pros Worldwide Help | Site Map

Matrix pairings to print 0 or 1

Newbie
 
Join Date: Jan 2008
Posts: 3
#1: 3 Weeks Ago
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

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my %A;
  5. my @order= sort keys %A;
  6.  
  7. while (<DATA>) {
  8.   warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/;
  9.   my ($row, $col) = ($1,$2);
  10.  $A {$row}{$col}++;
  11.   $A{$col}{$row}++;
  12.  
  13.  
  14. print "    ";
  15. printf(" %s",$_) for (@order);
  16. print "\n";
  17.  
  18.  
  19. for my $row (@order) {
  20.   printf(" %s", $row);
  21.  
  22.   for my $col (@order) {
  23.     printf(" %s",  exists $A{$row}{$col} ? "1" : "0" );
  24.   }
  25.   print "\n";
  26. }
  27.  
  28.  
  29. __DATA__
  30. horse;tiger
  31. lion;mule
  32. lion;tiger
  33. horse;mule
  34.  
  35.  
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.
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4. my %A;
  5. my @order=qw(horse mule tiger lion);
  6.  
  7. while (<DATA>) { 
  8.   warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/; 
  9.   my ($row, $col) = ($1,$2); 
  10.  $A {$row}{$col}++; 
  11.   $A{$col}{$row}++; 
  12.  } 
  13.  
  14. print "    "; 
  15. printf(" %s",$_) for (@order); 
  16. print "\n"; 
  17.  
  18. for my $row (@order) { 
  19.   printf(" %s", $row); 
  20.  
  21.   for my $col (@order) { 
  22.     printf(" %s",  exists $A{$row}{$col} ? "1" : "0" ); 
  23.   } 
  24.   print "\n"; 
  25. }
  26.  
  27.  
  28. __DATA__
  29. horse;tiger 
  30. lion;mule 
  31. lion;tiger 
  32. horse;mule
  33.  
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: 3 Weeks Ago

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.
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4. my %A;
  5. my @order=qw(horse mule tiger lion);
  6.  
  7. while (<DATA>) { 
  8.   warn("Line $.: cannot understand: $_") , next unless /(\w+);(\w+)/; 
  9.   my ($row, $col) = ($1,$2); 
  10.  $A {$row}{$col}++; 
  11.   $A{$col}{$row}++; 
  12.  } 
  13.  
  14. print "    "; 
  15. printf(" %s",$_) for (@order); 
  16. print "\n"; 
  17.  
  18. for my $row (@order) { 
  19.   printf(" %s", $row); 
  20.  
  21.   for my $col (@order) { 
  22.     printf(" %s",  exists $A{$row}{$col} ? "1" : "0" ); 
  23.   } 
  24.   print "\n"; 
  25. }
  26.  
  27.  
  28. __DATA__
  29. horse;tiger 
  30. lion;mule 
  31. lion;tiger 
  32. horse;mule
  33.  
Reply