Dharshana Eswaran wrote:
Hi all,

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

My source file, i.e., my .h file, contains statements like:

#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      */

Basically it defines a set of message names(GMMREG_DETACH_CNF_ID) to its hex
values((GMMREG_PRIM_ID_BASE | 0x0011) ). here GMMREG_PRIM_ID_BASE is
initialized to some value.

From this file i need to extract three strings from each line. they are
1. GMMREG_ATTACH_REQ_ID
2. GMMREG_PRIM_ID_BASE
3. 0x0001 => this string can be of 2 digits or 4 digits or sometimes it can
be a 2 digit decimal number too.

the 3rd string is sometimes is not present.

Once these are extracted, i can process it further to the requirement.

I tried reading each line, splitting it using spaces, grouping them using
pattern matching and then displaying. The code, which i wrote for this, is
written at the end of this mail.

But i am facing a issue like, the header file need not have spaces as in
every line.
A line can read like: #define GMMREG_DETACH_IND_ID (GMMREG_PRIM_ID_BASE | 
0x0030)  /* @LOG DETACH_IND  */

It can also read like: #define CCL2D_INIT_NCNF_ID (L2D_PRIM_ID_BASE+5)      /* 
@LOG CCL2D_INIT_NCNF      */

So because of the same all the patterns are not getting identified.

Since i am using spaces as delimiters here, i am facing problems.

I tried using "|" or "+" or "(" or ")", but i am unable to get a proper
working logic with these delimiters.

Can anyone suggest any solution for this?

[snip code]

I think the code below does what what you need. Note that the oct() function
takes notice of the prefix on a number supplied to it, so that if you pass
'0x0011' it will correctly return 17. Numbers not starting with a leading zero
are assumed to be decimal by default.

I hope this helps.

Rob



use strict;
use warnings;

while (<DATA>) {

 next unless /#define\s+(\w+)\s+\(([^)]+)/;
my $name = $1;
 my ($base, $offset) = $2 =~ /\w+/g;
if ($offset) {
   $offset = oct $offset if $offset =~ /^0/;
 }
 else {
   $offset = 0;
 }
printf "$name = $base + $offset\n";
}

**OUTPUT**

GMMREG_ATTACH_REQ_ID = GMMREG_PRIM_ID_BASE + 0
GMMREG_DETACH_REQ_ID = GMMREG_PRIM_ID_BASE + 1
GMMREG_ATTACH_CNF_ID = GMMREG_PRIM_ID_BASE + 16
GMMREG_DETACH_CNF_ID = GMMREG_PRIM_ID_BASE + 17
CCL2D_INIT_NCNF_ID = L2D_PRIM_ID_BASE + 5
CCL2D_INIT_RESP_ID = L2D_PRIM_ID_BASE + 6

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


Reply via email to