With your change, I've proven what I wanted to know, that a variable "created" by concatenation can be re-used. So, thanks for that, as I was in doubt because of the peculiar wording in the help:
This method does not change the existing strings, but returns a new string containing the text of the joined variables
I think what this really means to say is that it returns a separate variable, rather than a "new" one.
Your change also gave me an idea, which identified the problem with my original code.
Add 2 new variables:
Dim dummy1 as String * 16Dim dummy2 as String * 16
dummy1 = ""
dummy2 = ""
trail = Str(v_a) + Str(v_b) ' this will not compile
trail = Str(v_a) + dummy1 'this will compile AND yield the correct result
dummy2 = trail + Str(v_b)
trail = dummy2
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
. trail = trail = Str(v_a) + Str(v_b) ' this will not compile ' this will not compile
think it would confuse compiler....confuses moi
Str(v_a) + Str(v_b) is two strings added to one(???) not the string values added...er?
Last edit: stan cartwright 2017-12-10
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Below is a program that show the recommended method.
Why does this work? The two strings concatenated together need an assignent of the target string then concatenated the second string to the target string.
Why do we do this way? To mimimise the code size. When you concatenated str(numericvar) + str(anothernumericvar) the second str() attempts to use the same temp variable as the first str(). So, assign the first str() to the target and only add on str() at the time, We may enhance the compiler in the future to improve this. So.... use assign and then add one str() at a time.
; ----- Configuration
#chip mega328p,16
#option explicit
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
; ----- Variable Declaration
Dim trail as String * 16
Dim v_a,v_b,v_c,v_i as Byte
v_a = 0
v_b = 1
v_c = 9
; ----- Get the terminal ready
wait 1 s
; ----- Main
for v_i = 1 to 10
trail = Str(v_a)
trail = trail + Str(v_b) ' this is line 17
HSerPrint trail
HSerPrintCRLF
trail = trail + Str(v_c)
HSerPrint trail
HSerPrintCRLF
HSerPrintCRLF
next v_i
End
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, this is something very BASIC, I'm sure. Can anyone shed some light for me?
I get a compilation error:
Here's the code:
it compiles with
Hi Stan, thanks, it does indeed. However, it does not display the intended result: 01.
Also, the following needs to be added to the configuration to get the terminal to display the output:
With your change, I've proven what I wanted to know, that a variable "created" by concatenation can be re-used. So, thanks for that, as I was in doubt because of the peculiar wording in the help:
I think what this really means to say is that it returns a separate variable, rather than a "new" one.
Your change also gave me an idea, which identified the problem with my original code.
Add 2 new variables:
trail = Str(v_a) + Str(v_b) ' this will not compile
trail = Str(v_a) + dummy1 'this will compile AND yield the correct result
dummy2 = trail + Str(v_b)
trail = dummy2
. trail = trail = Str(v_a) + Str(v_b) ' this will not compile ' this will not compile
think it would confuse compiler....confuses moi
Str(v_a) + Str(v_b) is two strings added to one(???) not the string values added...er?
Last edit: stan cartwright 2017-12-10
I was thinking about it this way:
v_a is an integer
v_b is an integer
Str(v_a) is a string
Str(v_b) is a string
trail = Str(v_a) + Str(v_b) is two strings concatenated together, not added together. In other words,
"0" concat "1" is "01".
0 plus 1 is 1
this will sound snarky, but it isn't. What part of that confuses you? Or, describe how were you thinking about it.
It would seem you think more like the compiler than I do, so I'm interested.
Below is a program that show the recommended method.
Why does this work? The two strings concatenated together need an assignent of the target string then concatenated the second string to the target string.
Why do we do this way? To mimimise the code size. When you concatenated str(numericvar) + str(anothernumericvar) the second str() attempts to use the same temp variable as the first str(). So, assign the first str() to the target and only add on str() at the time, We may enhance the compiler in the future to improve this. So.... use assign and then add one str() at a time.
Thanks Anobium, that makes perfect sense. Also, I like the new ability to concatenate a variable to itself.
Check the Help. You can.
Yes, that's what I meant. I like it!