0

Why is this generating the error:

Use of uninitialized value $match in substitution (s///) at ...

my $sub = 0;  #added
my $m;  #added

open (FH1, "<FILE1.txt") || die $!;
open (FH2, "<FILE2,txt") || die $!;

my @strings = (<FH2>);  #each line of the file into an array
close FH2 or die $!;

my $here;
while ( my $url = <FH1> ) {
    chomp $url;

    foreach my $sub (@strings) {      
         my $repeat = 1; 
         while ((my $m = $_) =~ s|(?<![/])(?:[/](?![/])[^/]*){$repeat}[^/]*\K|$sub|)  #<-- Error states the error is occurring here
         {

              print "$m\n";   
              $repeat++;    

              push( @{ $here->{$url} }, $m ); 
         }        
    }
}

There is definitely something in the files (as I can print each of the lines inside the foreach loop) and the regular expression substitution definitely works as it has been tested in its own program before I tried to move it into this program.

Is it something obvious I'm overlooking?

Your help would be much appreciated, many thanks

4

3 回答 3

4

You never assign anything to $_.

while ((my $match = $_) =~ ...

should be

while ((my $match = $url) =~ ...
于 2013-02-27T17:08:37.570 回答
2

$match is uninitialized, as the error says. It is set to $_, which is uninitialized — you are explicitly providing loop variables ($url and $sub), so the implicit variable is not initialized in this case.

I assume you mean to ... ($match = $url) =~ s/the subst/.../ ...

于 2013-02-27T17:08:40.480 回答
0

@strings有一个未初始化的字符串。您正在尝试使用此字符串(通过$sub)来替换正则表达式。确保@strings有你想要的数据。

于 2013-02-27T17:02:23.927 回答