By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

Matrix pairings to print 0 or 1

P: 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

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?
Oct 29 '09 #1

✓ answered 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.  

Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
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.  
Oct 29 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.