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

后端 未结 6 933
星月不相逢
星月不相逢 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:20

    From perlop, Quote and Quote-Like operators [bits in brackets added by me]:

    /PATTERN/msixpodualgc

    Searches a string for a pattern match, and in scalar context returns true [1] if it succeeds, false [undef] if it fails.

    (Looking at the section on s/// will also be useful ;-)

    Perl just doesn't have a discreet boolean type or true/false aliases so 1 and undef are often used: however, it could very well could be other values without making the documentation incorrect.

    $1 will never be defined because there is no capture group: perhaps $& (aka $MATCH) is desired? (Or better, change the regular expression to have a capture group ;-)

    Happy coding.

提交回复
热议问题