Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

[SOLVED] Difference between `[]`, `{}` and `()` expressions?

Tue May 03, 2016 10:21 am

As a follow-up on Re: What parts of scripts can be ran from the console? as I'm wrapping my head around the language and the Scripting language manual is quite ambiguous in various places (a BNF or EBNF grammar would be really really helpful so if someone has it: please share).

Apparently, you can use the below three expression forms around statements, but the outcome is different:
admin@MikroTikCCR1009] > :put [/system clock get value-name=date]
may/03/2016
admin@MikroTikCCR1009] > :put {/system clock get value-name=date} 
true
admin@MikroTikCCR1009] > :put (/system clock get value-name=date) 
true
Why is that?
What's the difference between the `[]`, `{}` and `()` expressions? (i.e. are they all block statements, or a different construct? Where are they documented?).

--jeroen
Last edited by jeroenp on Thu May 05, 2016 11:59 am, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Difference between `[]`, `{}` and `()` expressions?

Tue May 03, 2016 4:02 pm

They're all documented on the very page you linked to, though not in the excruciating detail that a seasoned programmer might want to.

The "[]" executes the command(s) inside the command line given (you can only have one line per operator), and returns the return value from the last command. In this case, the "get" command inside returns the date, so that's what :put gets, and then outputs.

The "{}", when used as an expression, doesn't create a block, but instead creates an array... Each "command" is really a member in the array. And what you're getting by doing a command instead of an "actual" value (like a number or a quoted string) is what's formally called "undefined behavior".

The "()" simply takes in and returns an expression, as in "values", not commands. For the same reason as above, you get true, only this time, you're getting just one value, instead of an array of values. You'll notice it's a syntax error if you try to add an unquoted ";" in there, exactly because neither arrays nor commands are accepted here.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Difference between `[]`, `{}` and `()` expressions?

Tue May 03, 2016 5:51 pm

Thanks. I will keep asking questions (trying to be specific or/and have a Short, Self Contained, Correct Example), then in due time blog a post organising them in some more readable fashion.

Who is online

Users browsing this forum: No registered users and 25 guests