Connecting Tech Pros Worldwide Help | Site Map

using perl to print output

Newbie
 
Join Date: Mar 2008
Posts: 2
#1: May 19 '09
suppose u have file

File A

A -> G
C->D
A -> R
P->A

File B
A=1
C=2
D=3
E=4
F=5
G=6
H=7
I=8
K=9
L=10
M=11
N=12
P=13
Q=14
R=15
S=16
T=17
V=18
W=19
Y=20

From File A and File B
output shud be such that
In any one first one shud have -1 and second one shud have 1

So,
In the first one (A)shud have -1 and second one (G) shud have 1
so that
A -> G 1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

similarly C have -1 and D have 1
C->D 1:0 2:0 3:-1 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

A->R 1:-1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0

P->A P have -1 and A have 1
P->A 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my %hash;
  3. while(<FILEB>){
  4.         chomp;
  5.         my @tmp=split("=",$_);
  6.         $hash{$tmp[0]}=$tmp[1];
  7. }
  8.  
  9. open $fh,"<","FILEA";
  10. while(<$fh>){
  11.         chomp;
  12.         /([A-Z])\s*->\s*([A-Z])/;
  13.         print $_;
  14. print "\n";
  15. }
  16.         for(my $i=1;$i<=20;$i++){
  17.                 if($i == $hash{$1}){
  18.                         print " ",$i,":",-1;
  19.                 }
  20.                 elsif($i == $hash{$2}){
  21.                         print " ",$i,":",1;
  22.                 }
  23.                 else{
  24.                         print " ",$i,":",0;
  25.                 }
  26.         }
  27.         print "\n";
  28. }
  29.  
  30.  
But after running this perl command
the result displayed is
A->G 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
C->D 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
A->R 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0
P->A 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Whant can be possible solution?
Thanks
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: May 19 '09

re: using perl to print output


Quote:
From File A and File B
output shud be such that
In any one first one shud have -1 and second one shud have 1

So,
In the first one (A)shud have -1 and second one (G) shud have 1
so that
A -> G 1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

similarly C have -1 and D have 1
C->D 1:0 2:0 3:-1 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

A->R 1:-1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0

P->A P have -1 and A have 1
P->A 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0
I personally have no idea what any of that means.

This part of your code does nothing except print the lines to STDOUT that are in the file:

Expand|Select|Wrap|Line Numbers
  1. open $fh,"<","FILEA";
  2. while(<$fh>){
  3.         chomp;
  4.         /([A-Z])\s*->\s*([A-Z])/;
  5.         print $_;
  6. print "\n";
  7. }
  8.  
Is that what you wanted?
Newbie
 
Join Date: Jan 2008
Posts: 3
#3: May 19 '09

re: using perl to print output


Quote:

Originally Posted by KevinADC View Post

I personally have no idea what any of that means.

This part of your code does nothing except print the lines to STDOUT that are in the file:

Expand|Select|Wrap|Line Numbers
  1. open $fh,"<","FILEA";
  2. while(<$fh>){
  3.         chomp;
  4.         /([A-Z])\s*->\s*([A-Z])/;
  5.  
  6.         print $_;
  7. print "\n";
  8. }
  9.  
Is that what you wanted?

From file A
we have inputs
A -> G
C->D
A -> R
P->A

say first one A->G
so from file B
A =1 G=6

so 1: 2:0 3:0 4:0 6: 7:0 all 20 vectors will be zero except 1 and 6

Now A is first letter so format is denoted -1 and
G is second letter so format is denoted as 1
therefore
final output will be

1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Take another example
P->A

so P=13 and A=1
and first letter is -1 and second letter is 1

therefore final output will be

1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Tht is output format needed
Newbie
 
Join Date: Jan 2008
Posts: 3
#4: May 19 '09

re: using perl to print output


Quote:

Originally Posted by KevinADC View Post

I personally have no idea what any of that means.

This part of your code does nothing except print the lines to STDOUT that are in the file:

Expand|Select|Wrap|Line Numbers
  1. open $fh,"<","FILEA";
  2. while(<$fh>){
  3.         chomp;
  4.         /([A-Z])\s*->\s*([A-Z])/;
  5.  
  6.         print $_;
  7. print "\n";
  8. }
  9.  
Is that what you wanted?

From file A
we have inputs
A -> G
C->D
A -> R
P->A

say first one A->G
so from file B
A =1 G=6

so 1: 2:0 3:0 4:0 6: 7:0 all 20 vectors will be zero except 1 and 6

Now A is first letter so format is denoted -1 and
G is second letter so format is denoted as 1
therefore
final output will be

1:-1 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Take another example
P->A

so P=13 and A=1
and first letter is -1 and second letter is 1

therefore final output will be
P->A
1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:-1 14:0 15:0 16:0 17:0 18:0 19:0 20:0

Tht is output format needed
Reply