user3789221 user3789221 - 3 months ago 10
Perl Question

how to sum the value in a matrix element or within square brackets[ ]

suppose i have a value 2
to be added to the matrix or range having
[ 99 to 103 ]
[ 267 to 271 ]

what's is the method of doing it ?

expected output:[ 101 to 105 ]
[ 269 to 273 ].

i have already fetch the range ..here is the code.

enter code here
$input= $argv[0];
open($file,"$input");
while($line=<$file>)
{
$ranges.=$line if($line=~m/\[/);
print $ranges,
if
($line=~m/----------------------------------------------------------------------/);
}


i want to add 2 to the matrix .thanks for any help

input file:

>13pk_A: starts from 5
ITNDYRIRSALPTLKKVLTEGGSCVLMSHLGRPKGIPMAQAGKIRSTGGVPGFQQKATL
Total number of repeat(s) = 2
The length of repeat = 4
Number of residues in the repeat = 4
KKVL z0
[ 13 to 16 ]
[ 44 to 47 ]
----------------------------------------------------------------

Answer
use strict;

use warnings;

my $file = "path\\data.txt";

open (my $fh, "<", $file) or die "can't open this file";

my $number = 4;

while ( my $line = <$fh>){

    if ( $line =~ /\[\s(\d*)\sto\s(\d*)\s]/){

        my $first_range = int($1);
        $first_range = $1 + $number;

        my $second_range = int($2);
        $second_range = $2 + $number;

        print "\n[ $first_range to $second_range ]";
    }
}

The regex will store in $1 and $2 the two ranges.

$line =~ /\[\s(\d*)\sto\s(\d*)\s]/

Base on the input pattern [ number to number ].

int($1);

This will convert the string to integer.

$first_range = $1 + $number;

Adds to your range the number which you want ( i hardcoed it to be 4, my $number = 4).