On 3/8/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote:
Hi all,

I need to extract few strings from one file and paste it to another file.
snip

This doesn't seem like a good job for split.  The split function is
good for parsing X separated records where X is either constant or
simple.  What you have there a grammar.  Specifically a subset of the
C grammar for #define.  With grammars you want to use either a regex
or Parse::RecDescent depending on the complexity of the grammar.  In
this case the grammar is simple enough that a regex does fine.  I have
created a regex that parses your record into a name, base number,
operator, and modifying number.  The last two values are optional.  I
have used the x option on the regex to make it readable since it is so
large (anything bigger than 80 characters should probably use the x
option).  You can learn more about regexes in perldoc perlre and
perldoc perlretut.

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
       my ($name, $base, $op, $mod) = m{
               ^                     # start of string
               \s*                   # optional spaces
               \#define              # the start of the macro
               \s+                   # mandatory spaces
               (\w+)                 # capture the name of the macro
               \s*                   # optional spaces
               \(                    # the open paren
               \s*                   # optional spaces
               (
                       \w+ |
                       \d+ |
                       0x[a-fA-F0-9]
               )                     # capture a word, int, or hex
               \s*                   # optional spaces
               (?:
                       (             # capture the various int operators
                               [+-|^*/%] |
                               <<        |
                               >>
                       )
                       \s*           # optional spaces
                       (             # capture a word, int, or hex
                               \w+ |
                               \d+ |
                               0x[a-fA-F0-9])
               )?                    # but make the last two captures optional
               \)                    # the close paren
       }x;
       $op = $mod = '' unless defined $op;
       print;
       print "\tname is $name, base is $base, modified by $mod using $op\n"
               if $name;
}

__DATA__
#define GMMREG_ATTACH_REQ_ID          (GMMREG_PRIM_ID_BASE)
/* @LOG GMMREG_ATTACH_REQ       */
#define GMMREG_DETACH_REQ_ID          (GMMREG_PRIM_ID_BASE | 0x0001)
/* @LOG GMMREG_DETACH_REQ       */
/* GMMREG primitives sent by the MM sub-layer to the MMI */
#define GMMREG_ATTACH_CNF_ID          (GMMREG_PRIM_ID_BASE | 0x0010)
/* @LOG GMMREG_ATTACH_CNF       */
#define GMMREG_DETACH_CNF_ID          (GMMREG_PRIM_ID_BASE | 0x0011)
/* @LOG GMMREG_DETACH_CNF       */
#define CCL2D_INIT_NCNF_ID      (L2D_PRIM_ID_BASE+5)      /* @LOG
CCL2D_INIT_NCNF      */
#define CCL2D_INIT_RESP_ID      (L2D_PRIM_ID_BASE+6)      /* @LOG
CCL2D_INIT_RESP      */

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to