Perl: how to perform mathematical operations inside a command?

I am trying to position the terminal cursor with print

and\e

Syntax: print "\e[<row>;<col>H";

However, I want to use the value row

multiplied by 2.

Can I do this in perl without additional variable assignment? If not, I think that multiplying row

by 2 on another line isn't the worst thing ever ...

$row = 1; $col = 1;
print "\e[$row;${col}H";     # Correct syntax, works fine.
print "\e[$row*2;${col}H";   # Does not work
print "\e[($row*2);${col}H"; # Does not work

      


Bash equivalent: echo -ne "\e[$(($row*2));${col}H"

+3


source to share


4 answers


Try the following:

$row = 1; $col = 1;
print "\e[$row;${col}H";     # Correct syntax, works fine.
print "\e[".($row*2).";${col}H";   # Does not work
print "\e[".($row*2).";${col}H"; # Does not work

      



Perform off-line calculations and concatenate the result.

+5


source


printf can be used to cleanly separate your calculations in printable format:



$row = 1; $col = 1;
printf "\e[%d;%dH", $row, $col;
printf "\e[%d;%dH", $row*2, $col;

      

+6


source


Perl does not interpolate expressions, just variables and dereferencing (it evaluates expressions inside dereferencing). To get the desired result, you will need to evaluate the expression outside of the string using the concatenation operator .

:

print "\e[".($row * 2).";${col}H";  

      

Alternatively, you can use printf

:

printf "\e[%d;%dH", $row*2, $col;

      

As Diab Jerius pointed out, you can also use the Baby Cart "hidden operator".

+5


source


You can do this using Baby Cart

$row = 1; $col = 1;
print "\e[@{[ $row*2 ]};${col}H";

      

+1


source







All Articles