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"
source to share
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".
source to share