Community discussions

MikroTik App
 
bonnecomm
newbie
Topic Author
Posts: 38
Joined: Sat May 30, 2009 8:29 am

Can't concatenate variable and a string.?!

Tue May 02, 2023 7:36 pm

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 (1024*1024*$qplan);
But I'd like to know why I'm getting the errors when trying to use the previous.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can't concatenate variable and a string.?!

Tue May 02, 2023 8:35 pm

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.
 
bonnecomm
newbie
Topic Author
Posts: 38
Joined: Sat May 30, 2009 8:29 am

Re: Can't concatenate variable and a string.?!

Tue May 02, 2023 9:28 pm

$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 "$"?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can't concatenate variable and a string.?!

Tue May 02, 2023 9:34 pm

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
 
bonnecomm
newbie
Topic Author
Posts: 38
Joined: Sat May 30, 2009 8:29 am

Re: Can't concatenate variable and a string.?!

Wed May 03, 2023 6:32 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can't concatenate variable and a string.?!

Thu May 04, 2023 12:15 am

Why? The others are the same? For example PHP is like C?
 
bonnecomm
newbie
Topic Author
Posts: 38
Joined: Sat May 30, 2009 8:29 am

Re: Can't concatenate variable and a string.?!

Thu May 04, 2023 12:44 am

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can't concatenate variable and a string.?!

Thu May 04, 2023 10:43 am

I know, I know :roll:
 
User avatar
Larsa
Forum Guru
Forum Guru
Posts: 1041
Joined: Sat Aug 29, 2015 7:40 pm
Location: The North Pole, Santa's Workshop

Re: Can't concatenate variable and a string.?!

Thu May 04, 2023 3:57 pm

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

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).
 
User avatar
jbl42
Member Candidate
Member Candidate
Posts: 214
Joined: Sun Jun 21, 2020 12:58 pm

Re: Can't concatenate variable and a string.?!

Fri May 05, 2023 3:15 am

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 {}.

Who is online

Users browsing this forum: No registered users and 14 guests