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

using perl to print output

P: 2
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
May 19 '09 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
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?
May 19 '09 #2

P: 3
@KevinADC
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
May 19 '09 #3

P: 3
@KevinADC
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
May 19 '09 #4

Post your reply

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