Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageJerry 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
Post Follow-up to this messageOn 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.