What is the $1
? Is that the match found for (\d+)
?
$line =~ /^(\d+)\s/;
next if(!defined($1) ) ;
$paperAnnot{开发者_运维技巧$1} = $line;
You are right, $1
means the first capturing group, in your example that is (\d+)
Yep! It's a group match. Seeing the next
there, it's probably in a loop. However, a better way of handling what you have there would be to use a conditional and test the regex:
if ( $line =~ /^(\d+)\s/ ) {
$paperAnnot{$1} = $line;
}
or even better, give $1 a name to make it self documenting:
if ( $line =~ /^(\d+)\s/ ) {
my $index = $1;
$paperAnnot{$index} = $line;
}
Also, you can find more about $1
, and its brethren in perldoc perlvar.
And now in Perl 5.10 and newer, you can use named capture groups:
use 5.010; # or newer
...
if ( $line =~ /^(?<linenum>\d+)\s/ ) {
$paperAnnot{ $+{linenum} } = $line;
}
See more on Named Capture Groups with perldoc perlre.
Yep, anything captured in parentheses is assigned to the $1, $2, $3... etc magic variables. If the regexp doesn't match they'll be undefined.
精彩评论