Search code examples
perlfloating-pointscientific-notation

How do I make Perl’s system() format arguments using scientific notation?


From my Perl program, I am trying to run another command written in C using system. The command requires several arguments: a string, a float, two integers, a pair of floats, and another string. I am running this as

my $arg1="electron";
my $arg2=0.511;
# more definitions
system("./fermions $arg1 $arg2 $arg3 $arg4 " .
       "$arg5 $arg6 \"string\" > outfile.out");

I need to vary $arg5 to be several different float values. I made it work by setting $arg5="1.0e5" and then running an if-statement in the for-loop to change the value as a string. I would like to be able to do this as floats, and tried

system("./fermions $arg1 $arg2 $arg3 $arg4 " .
       "%e $arg6 \"string\" >outfile.out",
       $arg5);

but that did not work. Is there another alternative, or is my if-statement option the only one?


Solution

  • If you want to use printf notation (like "%e"), you need to use the Perl sprintf builtin. Otherwise you just end up passing "%e" as a literal argument.