Jerry Preston wrote:

> Hi!
> 
> I am trying to break down the following:
> 
>    printf("numsteps=%d  i=%d im=%g vfr=%g \n",numsteps,i,imeas,vforce);
> into
>    "numsteps= numsteps  i=i im=imeas vfr=vforce \n"
> 
>    printf ("\noriginal cap = %g, offset = %g", *ci, cap_offset);
> into
>    "\noriginal cap = ci, offset = cap_offset";
> 
> I am wanting to improve my limited regex skills.  Is the above possible or
> is there a better way with one method?

This can be done using regexes, there might be better methods out there 
that I am not aware of. I guess this should get you started.

Note: I have not tested this for all scenarios and this will require you to 
do some more work. The obvious one being, handling spaces.

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
    chomp;

    #Remove unwanted chars at the beginning
    my $format_str = $1 if (s/^printf\(?"(.*)",?//);
    next unless (defined ($format_str));
    #Remove unwanted chars at the end
    s/\)?;//;
    #Get the parameters list
    my @params     = split (',');

    $format_str    =~ s/\%\w/shift (@params)/ge;
    print $format_str, "\n";
}

__DATA__
printf("Hello World");
printf("a=%d b=%f c=%r\n", a, b, c);
printf("a=%d\n", a, b);
printf("numsteps=%d  i=%d im=%g vfr=%g \n",numsteps,i,imeas,vforce);
printf("\noriginal cap = %g, offset = %g", *ci, cap_offset);
printf("");


Output
Hello World
a= a b= b c= c\n
a= a\n
numsteps=numsteps  i=i im=imeas vfr=vforce \n
\noriginal cap =  *ci, offset =  cap_offset


> 
> Thanks,
> 
> Jerry


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to