开发者

How to add an alphabet at the end of each line and should increment it for the next line and so on

开发者 https://www.devze.com 2023-03-14 17:27 出处:网络
I have the code as follows: #!/usr/local/bin/perl use warnings; use strict; my $inputfile = \"file1.txt\";

I have the code as follows:

#!/usr/local/bin/perl
use warnings;
use strict;
my $inputfile = "file1.txt"; 
open FH,$inputfile;          

my @results = <FH>;  

close FH;           
print "\n---------type n--------------\n" ;
foreach my $line (@results) {
           if($line =~ m/(^Mgn\d\.qna.*)/i)            

                   {
                          print "$1\n";  
           }
        }
print "\n---------type p--------------\n" ;
foreach my $line (@results) {
           if($line =~ m/(^Mgp\d\.qpa.*)/i)     
                 print "$1\n"; 
          }
      }

output: from the above script

---------type n--------------

Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$UNI

Mg3.qna o a vss vss n 0.36 0.03 mult=8 $$UNI

Mg5.qna o a vss vss n 0.36 0.03 mult=6 $$UNI

Mg7.qna o a vss vss n 0.36 0.03 mult=4 $$UNI

Mg9.qna o a vss vss n 0.36 0.03 mult=34 $$UNI

---------type p--------------

Mg2.qpa o a vcc vcc p 0.36 0.03 mult=6 $$UNI

Mg4.qpa o a vcc vcc p 0.36 0.03 mult=4 $$UNI

Mg6.qpa o a vcc vcc p 0.36 0.03 mult=8 $$UNI

Mg8.qpa o a vcc vcc p 0.36 0.03 mult=34 $$UNI

I need output as folllows

---------type n--------------

Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$UNI a

开发者_JAVA技巧

Mg3.qna o a vss vss n 0.36 0.03 mult=8 $$UNI b

Mg5.qna o a vss vss n 0.36 0.03 mult=6 $$UNI c

Mg7.qna o a vss vss n 0.36 0.03 mult=4 $$UNI d

Mg9.qna o a vss vss n 0.36 0.03 mult=34 $$UNI e

---------type p--------------

Mg2.qpa o a vcc vcc p 0.36 0.03 mult=6 $$UNI f

Mg4.qpa o a vcc vcc p 0.36 0.03 mult=4 $$UNI g

Mg6.qpa o a vcc vcc p 0.36 0.03 mult=8 $$UNI h

Mg8.qpa o a vcc vcc p 0.36 0.03 mult=34 $$UNI i


Made the code compile. Fixed your problem. Introduced a few modern Perl idioms.

#!/usr/bin/env perl

use warnings;
use strict;

my $inputfile = 'file1.txt';

open my $infh, '<', $inputfile or die "Can't open $inputfile: $!\n";          

my (@type_n, @type_p);

while (<$infh>) {
   push @type_n, $_ if /(^Mgn\d\.qna.*)/i;
   push @type_p, $_ if /(^Mgp\d\.qpa.*)/i;
}

close $infh;

my $chr = 'a';

print "\n---------type n--------------\n" ;
foreach (@type_n) {
  print "$_ " . $chr++ . "\n";  
}

print "\n---------type p--------------\n" ;
foreach (@type_p) {
  print "$_ " . $chr++ . "\n";  
}


$counter = 0; #at the top of your loop.


# print this at the end of each line.
# it cycles through the letters and then goes back to a using % (modulus)
print chr(ord('a') + ( $counter % 26));


This doesn't address all of the issues that you might need to consider, but here is a different way to think about your script:

my @regexes = (
    qr/(...)/,
    qr/(...)/,
);

for my $r (@regexes){
    my $letter = 'a';
    my @matches = map { $_ =~ $r ? $1 : () } @results;
    print join(' ', $_, $letter ++), "\n" for @matches;
}


You may use something like that..

bash-3.2$ cat /tmp/$$
Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$UNI
Mg3.qna o a vss vss n 0.36 0.03 mult=8 $$UNI
Mg5.qna o a vss vss n 0.36 0.03 mult=6 $$UNI
Mg7.qna o a vss vss n 0.36 0.03 mult=4 $$UNI
Mg9.qna o a vss vss n 0.36 0.03 mult=34 $$UNI
Mg2.qpa o a vcc vcc p 0.36 0.03 mult=6 $$UNI
Mg4.qpa o a vcc vcc p 0.36 0.03 mult=4 $$UNI
Mg6.qpa o a vcc vcc p 0.36 0.03 mult=8 $$UNI
Mg8.qpa o a vcc vcc p 0.36 0.03 mult=34 $$UNI

bash-3.2$ perl -lne 'BEGIN{$val=a} print "$_", " $val"; ++$val'  /tmp/$$
Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$UNI a
Mg3.qna o a vss vss n 0.36 0.03 mult=8 $$UNI b
Mg5.qna o a vss vss n 0.36 0.03 mult=6 $$UNI c
Mg7.qna o a vss vss n 0.36 0.03 mult=4 $$UNI d
Mg9.qna o a vss vss n 0.36 0.03 mult=34 $$UNI e
Mg2.qpa o a vcc vcc p 0.36 0.03 mult=6 $$UNI f
Mg4.qpa o a vcc vcc p 0.36 0.03 mult=4 $$UNI g
Mg6.qpa o a vcc vcc p 0.36 0.03 mult=8 $$UNI h
Mg8.qpa o a vcc vcc p 0.36 0.03 mult=34 $$UNI i
0

精彩评论

暂无评论...
验证码 换一张
取 消