Multi Commander > Support and Feedback
Problem with String Concatenation
(1/1)
AlanJB:
Apologies if this is a long post for a simple problem, but I wanted to give as much info as possible.
This started as a simple diagnostic exercise to check that I was correctly getting the source paths of all 5 open tabs in the left explorer panel.
The script (note line 6 , beginning "$Paths...") is:
--- Code: ---@var $Paths = "";
@var $n = 0;
for ( $n=1; $n<6; $n++ )
{
MC.SetActiveTab PANEL=LEFT TAB={$n};
$Paths += GetSourcePath() + "\r\n";
}
MessageBox ("Output",$Paths,0)
--- End code ---
The Output, as expected, is:
--- Code: ---C:\Alan\
C:\Zip\
C:\Tools\
C:\Temp\
C:\Users\Alan\
--- End code ---
If I change line 6 to: $Paths += $n + GetSourcePath() + "\r\n"; the output, NOT as expected, is:
--- Code: ---12345
--- End code ---
It seems everything after $n is ignored in the concatenation !?
If I change line 6 to: $Paths += GetSourcePath() + " " + $n + "\r\n"; the output, again NOT as expected, is:
--- Code: ---C:\Alan\ 1C:\Zip\ 2C:\Tools\ 3C:\Temp\ 4C:\Users\Alan\ 5
--- End code ---
Once again, it seems everything after $n is ignored. Is this expected behaviour? Is there a workaround, or am I doing something wrong?
According to the documentation: "Conversion from string to number and number to string are often done automatically, making it possible to concatenate a number variable or constant to a string."
TIA
Alan
Mathias (Author):
I think is because $n is a number, so when you do + it tries to do math
Normally num converts to string automatically. But that is not possible in all situations.
But you can force a number into a string
--- Code: ---$Paths += numtostr($)n + GetSourcePath() + "\r\n";
--- End code ---
AlanJB:
I guessed that may be the case when further testing (after I posted) showed that if $n was the last argument in the concatenated string, it worked.
numtostring() works fine - thanks.
Luckily, no-one spotted my deliberate (ahem) mistake - the space in "MessageBox (", which will cause the function to fail, of course. You need to make the script engine smarter to allow for programmers like me ;D
Thanks again.
Alan
Navigation
[0] Message Index
Go to full version