Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: extracting data

by mirod (Canon)
on Jun 27, 2012 at 09:33 UTC ( #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.


Comment on Re: extracting data
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-07-31 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls