Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

extracting data

by prassi (Acolyte)
on Jun 27, 2012 at 06:29 UTC ( #978568=perlquestion: print w/replies, xml ) Need Help??
prassi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monk,

I have a .h file with the different macro definition like given below snippet

{ #define thread 1 #define flag #define code 0 #define const (100/5) #define flag_condn 1 }
I need to extract only variable which is assigned with 1 or 0 or blank space, like my out put has to be
{ thread flag code flag_condn }
Here is my perl which I am not able to get properly in case of #define flag which has a blank space, I used split function,
while (<INPUT>) { my @split_data = split(' ', $_); if($split_data[2] == " "||$split_data[2]==1||$split_data[2] == 0){ print "$split_data[1]\n" } }

regards, -Prassi

Replies are listed 'Best First'.
Re: extracting data
by moritz (Cardinal) on Jun 27, 2012 at 06:35 UTC

    Always use strict; use warnings; it would tell you what's wrong.

    There are two problems with your code. The first is that compare strings with ==, even though that operator does numeric comparison. And the second is that if there are only two chunks in the original string, $split_data[2] will not be the empty string, but undef.

    So your test should look more like

    if (!defined($split_data[2]) || $split_data[2] eq '0') { print $split_data[1], "\n"; }

      Thanks moritz.... yeah I dint realized the string comparison.

Re: extracting data
by frozenwithjoy (Priest) on Jun 27, 2012 at 06:54 UTC

    Just wanted to point out a slight typo-ish thing. You left out this condition:

    || $split_data[2] eq '1'

    edit: oops, this was supposed to be in response to moritz's helpful comment.

Re: extracting data
by mirod (Canon) on Jun 27, 2012 at 09:33 UTC

    Do you really need to use split? Why not match exactly what you want in the main loop?

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; while (<DATA>) { # should you need it, the value is in $2, undef if not present say $1 if m{^ \s* \#define \s+ (\S+) (?:\s+ (0|1))? \s* $}x; } __DATA__ #define thread 1 #define flag #define code 0 #define const (100/5) #define flag_condn 1

    A couple of notes: if you are using an ancient (pre 5.10) perl, then don't use use feature 'say'; and say, instead use print $1, "\n";. The /x modifier allows nicer formating for the regexp: whitespace is ignored, comments are allowed (hence the \# here, since it's not a comment) and the regexp can be spread over several lines.

Re: extracting data
by kcott (Chancellor) on Jun 27, 2012 at 08:18 UTC

    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


    $ 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

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://978568]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-05-29 18:33 GMT
Find Nodes?
    Voting Booth?