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

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


Reply via email to