userbio
userbio

Reputation: 125

Subtract pattern matching from array element perl

I have written a code which allows me to subtract a specific value (ex: FP=0.021) from one element from an array if it matches a specific pattern. Here it is the code:

if ($info =~ /FP=/) {
            my @array1 = split(';', $info);
                if ($array1[$#array1] =~ /=([^.]*)/){
                    my $name1= $-[1];
                    $FPvalue = substr($array1[$#array1], $name1);
                    if ($FPvalue < 0.0001){ 
                        push(@FPvalues,$FPvalue);

Where $info is a string which contains information separated by a semicolon character (;).

I am lucky and the "FP=0.021" element is the last element from my array. But I would like to know a way for subtract it without using the expression: $array1[$#array1]

I would appreciate your help, thanks!

Upvotes: 0

Views: 204

Answers (2)

Borodin
Borodin

Reputation: 126742

It is hard to tell without sample input data, but I think you want

push @FPvalues, $1 if $info =~ /FP=([\d.]+)/

It works by searching the string in $info for the sequence FP= followed by a number of dots and decimal digits. If that pattern is found, then the dots and digits part is put into $1 and pushed onto the array.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

Here how you can parse the decimal number from the string as it resides at the end of the string:

$str = "asdsa;adsasd;adsasd;FP=0.021";
if($str =~ /=(\d+\.?\d+)$/){
    print $1;
}

Upvotes: 1

Related Questions