Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: extracting data

by mirod (Canon)
on Jun 27, 2012 at 09:33 UTC ( [id://978603]=note: print w/replies, xml ) Need Help??


in reply to extracting data

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://978603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-18 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found