in reply to extracting data

It looks like ++moritz has identified your problems.

You might find coding it like this, is a little more readable:

#!/usr/bin/env perl use strict; use warnings; while (<DATA>) { my (undef, $def, $val) = split; print "$def\n" if ! $val or $val eq '1'; } __DATA__ #define thread 1 #define flag #define code 0 #define const (100/5) #define flag_condn 1

Output:

$ pm_define_extract.pl thread flag code flag_condn

Update: I changed the print line following moritz' comments below. It use to be:

print "$def\n" if ! $val or $val =~ /^0|1$/;

-- Ken

Replies are listed 'Best First'.
Re^2: extracting data
by moritz (Cardinal) on Jun 27, 2012 at 08:25 UTC
    print "$def\n" if ! $val or $val =~ /^0|1$/;

    Note that !$val already covers the case where $val is 0.

    Also /^0|1$/ is not what you might think it is. The OR | in regexes has a looser precedence than concatenation, which means the regex is the same as ^0 OR 1$, so it also matches strings like 0foo or bar1.

    The correct way to write it is /^(0|1)$/, or if you don't want to create a capture, /^(?:0|1)$/.

      ++ Thanks for spotting that. I've updated my node.

      I wasn't aware of the precedence issue: thanks for the good explanation.

      -- Ken