Perl match only returning “1”. Booleans? Why?

后端 未结 6 936
星月不相逢
星月不相逢 2021-01-18 13:18

This has got to be obvious but I\'m just not seeing it.

I have a documents containing thousands of records just like below:

Row:1 DATA:
[0]37755442
[         


        
6条回答
  •  耶瑟儿~
    2021-01-18 13:36

    If you want the result of a match as "true" or "false", then do the pattern match in scalar context. That's what you did in your first example. You performed a pattern match and assigned the result to the scalar my($foo). So $foo got a "true" or "false" value.

    But if you want to capture the text that matched a part of your pattern, use grouping parentheses and then check the corresponding $ variable. For example, consider the expression:

    $record =~ /(.*)ing/
    

    A match on the word "speaking" will assign "speak" to $1, "listening" will assign "listen" to $1, etc. That's what you are trying to do in your second example. The trouble is that you need to add in the grouping parentheses. "$record =~ /Defect/" will assign nothing to $1 because there are no grouping parentheses in the pattern.

提交回复
热议问题