Can't concatenate variable and a string.?!

RouterOS 6.49.7 (stable)

Here’s what I’m trying to do:
:set $qid [/queue simple get value-name=name $q];
:set $qq [:pick $qid 0 1];
:set $qplan [:pick $qid 1 3];
:set $qplan [:tonum $qplan];
:set $qpm [:tostr $qplan].“M”;
:if ($qplan>0) do={ /queue simple set [find name=$qid] max-limit=“0/$qpm” burst-limit=“0/0” burst-threshold=“0/0” burst-time=“0/0”; }

:set $qpm [:tostr $qplan].“M”; keeps giving me an error saying it expects the line to terminate before the .“M”.

I tried replacing it with:
:set $qpm [:tostr $qplan];
:set $qpm $qpm.“M”;
but I get an error on the second line of that.

I’ve worked around it by using:
:set $qpm (10241024$qplan);
But I’d like to know why I’m getting the errors when trying to use the previous.

It’s all wrong, just some:

$q at start not exist
$ must not be used on global, local and set

why uselessly convert to num and conver to a string?
for add a “M” at the end:
:set qpm "$“qplan"M”

etc.

$q comes from the lines I didn’t include in order to keep things concise to the problem:
:local qid;
:local qplan 0;
:local qq;
:local qpm;
:foreach q in=[/queue simple find parent!=none] do={

The Queue names are things like V05_172.16.20.5, W03_172.16.20.14, H07_172.16.20.5, X10_172.16.20.15, B18_172.16.20.5, and S25_172.16.20.5.
So the goal with qpm is to get values like “5M”, “3M”, “7M”, “10M”, “18M”, and “25M”. We need to get rid of the leading 0.
And we want to use numerical values for qplan so we can compare to 0 which is why it needs to be numerical.

While your code works, it seems strange. Why is there “$”?

???


Is not “$”, is "$“qplan"M”

"$“qplan"M”

a string between quotes " ", the quotes start and stop the string
a variable $“qplan”, read the variable qplan and put it’s value on the string, quotes are used for not mix variable name qplan with M
a character M, simply put the letter M inside the string

Ok. Thanks.

Mikrotik scripting language is a strange beast, nothing like C, C++, C#, Java, PHP, JavaScript, Fortran, Cobol, Forth, 8086, M68k, 6800, TMS740, TMS720, 8051, or any of the other languages I know.

Why? The others are the same? For example PHP is like C?

Every language has its peculiarities.
Java is very like C++. C++ is an extension of C so very similar.
PHP has a very C-like syntax and uses much of the C library. It also borrows extensively from Java and C++.
JavaScript is kind of similar to Java.
Fortran, Cobol, and Forth are like nothing else, especially Forth.
And Assembly is its own beast but all, except ARM, generally follow similar conventions.

But MikroTik script has a syntax all its own. It’s certainly not like C or PHP. In some ways its closer to Assembly.

To deal with the problem in PHP, it would be:
$qplan = intval(substr($qid,1,2));
$qpm = sprintf(“%dM”,$qplan);
or
$qplan = intval(substr($qid,1,2));
$qpm = strval($qplan).“M”;

In C, C++, C#, or Java, it would be:
int qplan;
char qpm[4];
qplan = int(substr(qid,1,2));
qpm = sprintf(“%dM”,qplan);

In JavaScript, it would be:
var qplan = Number(qid.substr(1,2));
var qpm = String(qplan) + “M”;

As you can see, none of them look anything like MikroTik Scripting.

I know, I know :unamused:

Rumor has it that we will get a full-fledged MyPython in v7.11! :slight_smile:

But to be honest, I’d be happy if it was just a real implementation of Nim, Lua, Tcl or something less crippled than the current RoS half-baked version of a scripting language (whatever it was based on).

But MikroTik script has a syntax all its own. It’s certainly not like C or PHP. In some ways its closer to Assembly.

Mikrotik scripts is basically shell automation and hence is more similar to bash/zsh (Unix shell) scripts. There you would write

"${qplan}M"

The difference is MT scripts use “” to enclose the variable name to be expanded, whereas bash/zsh use {}.