I am searching for a string in Perl and storing it in another scalar variable. I want to print this scalar variable. The code below doesn’t seem to work. I am not sure what is going wrong and what is the right way to go about it. Why is it printing ‘1’ when it doesn’t exist in the program?
Data it is running on
DATA
13 E 0.496 -> Q 0.724
18 S 0.507 -> R 0.513
19 N 0.485 -> S 0.681
21 N 0.557 -> K 0.482
The following is my code:
#!/usr/bin/perl
use strict;
use warnings;
my $find = '\s{10}[0-9]{2}\s[A-Z]'; #regex. it can also be '\s{10}[0-9]{2}\s[A-Z]'
#both dont seem to work
my @element;
open (FILE, "/user/run/test") || die "can't open file \n";
while (my $line = <FILE>) {
chomp ($line);
print "reached here1 \n"; # to test whether it reading the program properly
my $value = $line=~ /$find/ ;
print "reached here 3 \n"; # to test whether it reading the program properly
print "$value \n";
}
exit;
OUTPUT
reached here1
1
reached here 3
reached here1
1
reached here 3
The regex matching operation returns true (1) for match success, false otherwise. If you want to retrieve the match, you should try one of the following:
($m1, $m2) = $string =~ /$regex/Note that you need to use captures in your regex for these to work. Which you’re not doing yet.
You ought to take a look at the complete documentation in perlop, section “Regexp Quote-Like Operators”