Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Regex to breakdown c code
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





Report this thread to moderator Post Follow-up to this message
Old Post
Jerry Preston
03-29-05 08:57 AM


Re: Regex to breakdown c code
Jerry Preston wrote:
> I am want to break down the following code that can have any number of var
s
> 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 ");"?[/color
]

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



John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
03-29-05 08:56 PM


Re: Regex to breakdown c code
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 var
s
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
03-29-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:53 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.