boen_robot mentioned he’d be working on a var_dump function, but never finished.
Lacking response there, I’ve started myself. The result is a gist with file named Function.varDump.rsc: https://gist.github.com/jpluimers/7c74fd85d579a69b87fe3c907c3d1e81
What I need are more test cases for data types and input on how to present them.
Some sample output is below.
Note I see very little reaction on my previous requests for input on scripts.
Please do consider giving me feedback (even “well done” is fine, but ideas for improvements are always better), as that keeps me motivated publishing this in the first place.
Lack of input will likely mean I’ll put updates to these scripts in a non-public repository.
# Examples:
# /system script run Function.varDump.rsc
# > $varDump value="12345"
# :typeof=str;len=5;$value=12345
## (yes, a value without parenthesis is considered a string)
# > $varDump value=12345
# :typeof=str;len=5;$value=12345
# > $varDump value=(12345)
# :typeof=num;$value=12345
# > $varDump
# :typeof=nothing
# > $varDump value=$fooBar
# :typeof=nothing
# > $varDump value=[$fooBar]
# :typeof=nil
#> $varDump value=({1;2;3} , 5 )
#:typeof=array;len=4
# :typeof=num;$value=0
# [0]:typeof=num;$value=1
# :typeof=num;$value=1
# [1]:typeof=num;$value=2
# :typeof=num;$value=2
# [2]:typeof=num;$value=3
# :typeof=num;$value=3
# [3]:typeof=num;$value=5
#> :local convert ({}); :set ($convert->("a\00")) ("A"); :set ($convert->("b")) ("B\01"); $varDump value=$convert;
#:typeof=array;len=2
# :typeof=str;len=2;$value=a
# $value escaped='a\00'
# [a\00]:typeof=str;len=1;$value=A
# :typeof=str;len=1;$value=b
# [b]:typeof=str;len=2;$value=B
# [b]$value escaped='B\01'
–jeroen
Speaking of repositories, why not collect all these scripts in a repository (instead of gists)?
Heck, rather than giving feedback, people* might contribute pull requests with more code.
As for feedback on this, like I mentioned, I personally prefer the more brief format of PHP’s var_dump(), i.e. my ideal output of all examples would be
str(5) "12345"
num(12345)
nothing
nil
array(4) {
[0] => str(2) "a\00"
[1] => str(1) "b"
[2] => str(2) "B\01"
}
Or I guess if you also imagine an associate array with keys “x”, “y” and “z”, that would be:
array(4) {
[0] str(1) "x" => str(2) "a\00"
[1] str(1) "y" => str(1) "b"
[2] str(1) "z" => str(2) "B\01"
}
Which is different from PHP, but that’s because in PHP, keys are EITHER numbers or strings (meaning array members can be reached by only one or the other), whereas RouterOS uses positions (the “” parts), and there’s potentially a key at each position, i.e. an array member may be reached by either means. => A good var_dump() function needs to tell you both.
The part I reached on my own was only the above with regards to scalar values and non-associative single level arrays. My problems started when trying to real with nested arrays (arrays inside array values), which work as you’d expect when it comes to “:pick”, but at least at the time I first attempted this, it was “:foreach” that was buggy - it looped over inner members, as if they were top level members. I haven’t tried since.
- Truth be told, when I say “people”, I mean me, and maybe two or three other forum members in total… As you’ve undoubtedly figured by now, people delving in both RouterOS scripting and programming extensively enough are a rare breed. So rare in fact that it was clear to me you’re it from the first time I replied to you
.
Thanks for the compliment.
Now I need to figure out an efficient way to get a repository going.
After that, I will revisit varDump.
–jeroen