Hello. I am relatively new to perl, and I am having a problem involving the evaluation of arithmetic operators enclosed in backticks. From within my perl program, I call another perl program, using the following lines of code:
which for clarity I will rewrite equivalently as:
However, in the interest of creating code as tiny as possible, I wish to change the system call to be inside backticks (this eliminates the necessity for the concatenations). A problem arises when I move the $n[0] - $i and $n[1] - 1 inside the backticks.
This works:
But this does not:
and putting parentheses around the operations doesn't help. Perl reads the minus signs not as an instruction to subtract but as the symbol itself, resulting in, for example, "4-2" instead of "2". Is there any way to force perl to recognise the subtraction inside the backticks? An escape symbol of some kind?
Thank you very much for any help.
Grape
PS) If you are curious, below is my entire program. It seeks all nonnegative integer solutions to the equation N = N_1 + N_2 + ... + N_n, where N is a nonnegative integer and n is a positive integer. It is written very poorly and calls itself inside the map loop (the file containing the code must be called p), but this is ignored because the intent is to end up with code that is as short as possible. Currently 103B
It takes two parameters, N and n (e.g., perl p 4 3).
Code:
@n=@ARGV;
map system("perl other.pl ".($n[0]-$_)." ".($n[1]-1)." $n[2]$_"), 0..$n[0];
which for clarity I will rewrite equivalently as:
Code:
@n=@ARGV;
for $i (0..$n[0]) {
system("perl other.pl ".($n[0]-$i)." ".($n[1]-1)." $n[2]$i");
}
However, in the interest of creating code as tiny as possible, I wish to change the system call to be inside backticks (this eliminates the necessity for the concatenations). A problem arises when I move the $n[0] - $i and $n[1] - 1 inside the backticks.
This works:
Code:
for $i (0..$n[0]){
$j = $n[0] - $i;
$k = $n[1] - 1;
print `perl other.pl $j $k $n[2]$i`;
}
But this does not:
Code:
for $i (0..$n[0]){
print `perl other.pl $n[0]-$i $n[1]-1 $n[2]$i`;
}
and putting parentheses around the operations doesn't help. Perl reads the minus signs not as an instruction to subtract but as the symbol itself, resulting in, for example, "4-2" instead of "2". Is there any way to force perl to recognise the subtraction inside the backticks? An escape symbol of some kind?
Thank you very much for any help.
Grape
PS) If you are curious, below is my entire program. It seeks all nonnegative integer solutions to the equation N = N_1 + N_2 + ... + N_n, where N is a nonnegative integer and n is a positive integer. It is written very poorly and calls itself inside the map loop (the file containing the code must be called p), but this is ignored because the intent is to end up with code that is as short as possible. Currently 103B

Code:
@n=@ARGV;$n[1]<2?print$n[2].$n[0].$/:map system("perl p ".($n[0]-$_)." ".($n[1]-1)." $n[2]$_"),0..$n[0]