Operating System - HP-UX
1846602 Members
1794 Online
110256 Solutions
New Discussion

Re: parsing c file using perl script

 
Poornima
New Member

parsing c file using perl script

Hi,

I am newbie to perl script programming.
I have given a c file, that has two arrays like

const static array1
{1, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
};

const static array2= {
{"104804",339},
{"106546",999},
{"106547",999}
};

I want to know how to extract the only first array using perl script

I have used the follwing regular expression to parse the elements like this.

I have my file contents in $str.

@struct= $str=~m/$structure\[\].*};/g;
@struct_lines = $struct[0]=~m/[ ]*{[ -:{,\t\"_A-Za-z0-9.]*}/g;

But when I print the $struct[0], it is extracting all the array elements.

Could anybody help me in extracting only the first array?

Regards,
Poornima.
3 REPLIES 3
Ermin Borovac
Honored Contributor

Re: parsing c file using perl script

@struct= $str =~ m/const static[^;]*};/g;

Something like this will leave you will array1 in $struct[0], array2 in $struct[1] etc.
H.Merijn Brand (procura
Honored Contributor

Re: parsing c file using perl script

First of all, you have the file content in $str, but that includes the newlines. Newlines are *not* matched by . in regular expressions, unless you add /s modifier.

Secondly, (i hope you typoed) the first defenition is illegal: it misses '= {' on the declaration line. But assuming it was there in the first place,

my %arr;
while ($str =~ m/\bconst\s+static\s+(\w+)\s*=\s*{(.*?)};/gs) {
$arr{$1} = $2;
}

will place array1 in $arr{array1}, array2 in $arr{2} etc

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Poornima
New Member

Re: parsing c file using perl script

Thanks guys,

That solved my problem.

-Poornima.