For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Regex to breakdown c code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regex to breakdown c code
Jerry Preston

2005-03-29, 3:57 am

I am want to break down the following code that can have any number of vars
being passed:

c_routine("name_1", 2, 3, 5, 0,9, 1, A, b, -1.5, 1.2, 100000, 0.03, 0, 0,
1, 0, m, l, var_name_1, var_name_2, var_name_3, var_name_4, var_name_5,
var_name_6);

I only want the passed vars.

my ( $nuts, $line ) = split /\(/;
my @passed_vars = split /,/;

Also is it possible to get only the vars following ", m, l, " through ");"?

Thanks,

Jerry




John W. Krahn

2005-03-29, 3:56 pm

Jerry Preston wrote:
> I am want to break down the following code that can have any number of vars
> being passed:
>
> c_routine("name_1", 2, 3, 5, 0,9, 1, A, b, -1.5, 1.2, 100000, 0.03, 0, 0,
> 1, 0, m, l, var_name_1, var_name_2, var_name_3, var_name_4, var_name_5,
> var_name_6);
>
> I only want the passed vars.
>
> my ( $nuts, $line ) = split /\(/;
> my @passed_vars = split /,/;


This should work (untested):

my $code = ' c_routine("name_1", 2, 3, 5, 0,9, 1, A, b, -1.5, 1.2, 100000,
0.03, 0, 0, 1, 0, m, l, var_name_1, var_name_2, var_name_3, var_name_4,
var_name_5, var_name_6);';

my @passed_vars = map split( /\s*,\s*/ ), $code =~ /\w+\s*\(([^)]+)\)/;


> Also is it possible to get only the vars following ", m, l, " through ");"?


my @passed_vars = map split( /\s*,\s*/ ), $code =~ /\w+\s*\([^)]*?, m, l,
([^)]+)\)/;



John
--
use Perl;
program
fulfillment
Offer Kaye

2005-03-29, 3:56 pm

On Tue, 29 Mar 2005 00:06:55 -0600, Jerry Preston wrote:
> I am want to break down the following code that can have any number of vars
> being passed:
>
> c_routine("name_1", 2, 3, 5, 0,9, 1, A, b, -1.5, 1.2, 100000, 0.03, 0, 0,
> 1, 0, m, l, var_name_1, var_name_2, var_name_3, var_name_4, var_name_5,
> var_name_6);
>
> I only want the passed vars.
>
> my ( $nuts, $line ) = split /\(/;
> my @passed_vars = split /,/;
>


Assuming the scalar $str holds the string 'c_routine("na..._6);', the
following 2 lines will get you all of the args:
my ($args_str) = $str =~ m/\((.+)\)/;
my @args = split /\s*,\s*/,$args_str;

The first line takes the result of matching against $str and saving
everything between the "(" and ")", and saves it into a new scalar
called $args_str. Note that the usage using parenthesis - "my
($args_str)", is required here, since the match operator returns
success/failure in scalar context, rather than the actual result,
which is what we are after.
The second line splits the resulting string into the parts seperated
by commas. We don't want the whitespaces around the commas included in
the arguments saved into @args, so we use /\s*,\s*/ rather than simply
"/,/". Another way would be to split with just /,/, and then manually
remove the redundant whitespace, using e.g. map:
my @args = map {s/\s//g;$_} split /,/,$args_str;

> Also is it possible to get only the vars following ", m, l, " through ");"?
>


my ($args_str2) = $str =~ m/(m,.+)\)/;
my @args_from_m_only = split /\s*,\s*/,$args_str2;

Hope this helps,
--
Offer Kaye
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com