Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluating Arithmetic Operators within Backticks 3

Status
Not open for further replies.

ProGrape

Programmer
Joined
Apr 26, 2007
Messages
4
Location
CA
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:

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 :) It takes two parameters, N and n (e.g., perl p 4 3).

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]
 
creating code as tiny as possible is your first mistake. Write good code, which is not always the least amount of code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hm, I have already acknowledged that I am writing the code poorly. That was not my question, and in general I use good programming practices. I can see instances where having operators evaluated properly inside backticks would be useful and convenient even if I were not trying to minimise the size of the code. If you wish, ignore the fact that in this instance I am merely trying to write extremely short code, and know that I am also interested in the answer for future applications when this is not the case.
 
I don't think what you are trying to do is going to work well inside of the backticks. The are being evaulated just like they would if you type it out in the shell.

You are asking the shell to try and do your math for you.

Why not do your working example where perl does the math and you just pass the computed values to the next script.

You can use expr (in unix) to tell the shell to do math... but again.. kinda defeats the purpose of using perl :)
 
instead of just "map system(...) 0..$n[0]"

you can enclose the system/backtick inside a block, and assign your $j & $k there....

map {$j=$n[0]-$i;$k=$n[1]-1;print `perl other.pl $j $k $n[2]$i`;} 0..$n[0]

untested....
 
Starting from 103B

Code:
[blue]@n[/blue]=[blue]@ARGV[/blue][red];[/red][blue]$n[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red]<[fuchsia]2[/fuchsia]?[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][blue]$n[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red].[blue]$n[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red].[blue]$/[/blue]:[url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][red]"[/red][purple]perl p [/purple][red]"[/red].[red]([/red][blue]$n[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red]-[blue]$_[/blue][red])[/red].[red]"[/red][purple] [/purple][red]"[/red].[red]([/red][blue]$n[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red]-[fuchsia]1[/fuchsia][red])[/red].[red]"[/red][purple] [blue]$n[/blue][2][blue]$_[/blue][/purple][red]"[/red][red])[/red],[fuchsia]0..[/fuchsia][blue]$n[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red]

Now 90B: Saved 13 characters by changing to variable from an array into scalars.

Code:
[red]([/red][blue]$i[/blue],[blue]$j[/blue],[blue]$k[/blue][red])[/red]=[blue]@ARGV[/blue][red];[/red][blue]$j[/blue]<[fuchsia]2[/fuchsia]?[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][blue]$k[/blue].[blue]$i[/blue].[blue]$/[/blue]:[url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][red]"[/red][purple]perl p [/purple][red]"[/red].[red]([/red][blue]$i[/blue]-[blue]$_[/blue][red])[/red].[red]"[/red][purple] [/purple][red]"[/red].[red]([/red][blue]$j[/blue]-[fuchsia]1[/fuchsia][red])[/red].[red]"[/red][purple] [blue]$k[/blue][blue]$_[/blue][/purple][red]"[/red][red])[/red],[fuchsia]0..[/fuchsia][blue]$i[/blue]

Now 77B: Saved 13 characters by changing calling method to do since you're calling a perl script.

Code:
[red]([/red][blue]$i[/blue],[blue]$j[/blue],[blue]$k[/blue][red])[/red]=[blue]@ARGV[/blue][red];[/red][blue]$j[/blue]<[fuchsia]2[/fuchsia]?[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][blue]$k[/blue].[blue]$i[/blue].[blue]$/[/blue]:[url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url][red]{[/red][blue]@ARGV[/blue]=[red]([/red][blue]$i[/blue]-[blue]$_[/blue],[blue]$j[/blue]-[fuchsia]1[/fuchsia],[blue]$k[/blue].[blue]$_[/blue][red])[/red][red];[/red][url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url][red]'[/red][purple]p[/purple][red]'[/red][red]}[/red],[fuchsia]0..[/fuchsia][blue]$i[/blue]

I'll leave any more golfing up to you, because as Kevin has already stated, this is definitely a waste of time and effort.

- Miller
 
travs & brigmar, thanks for the suggestions. Yes I see what you mean about asking the shell to do the calculations, I hadn't really understood that before. And brigmar, that is what I had done (it works), but it results in longer code :P

Miller, Wow! Even though your second improvement doesn't quite work (even if you remove the comma after the } ), you've given me a couple of things to chew over that I didn't know about before! I'm actually glad the second one didn't work out, because now I can go and learn about the do command and figure it out for myself, and be able to take proper credit for work done :D

Oh, I know code golf is a waste of time, but it's my first shot at it and it's kind of fun.

Grape

PS) Why do they call it golfing?!
PPS) What's your secret for code highlighting? Mine's black.
 
It's called golfing I presume, because lower scores are better ?
 
That would make sense.

If anyone is interested, I wrote a few est scripts to analyse the behaviour of the do command. What was needed what the useage of my($i,$j,$k) instead of simply ($i,$j,$k). do and backticks do not work in exactly the same way... the calling instance of p continues to use the values of i, j, and k that the instance that it called was using when it finished. I haven't tested yet on using do to call scripts with different names but with variable names in common.

Thanks again.

see aye eye oh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top