Menu

Vexing problem with concatenation

Help
2017-12-09
2017-12-10
  • George Alvarez

    George Alvarez - 2017-12-09

    Hi, this is something very BASIC, I'm sure. Can anyone shed some light for me?

    I get a compilation error:

    Great Cow BASIC (0.98.01 2017-10-27)

    Compiling C:\GCB\GreatCowBasic\Demos_My Projects\contatenation.gcb ...

    Errors have been found:

    Error: Array SYSFNSTRING1 has not been declared
    contatenation.gcb (17): Error: Array SYSFNSTRING1 has not been declared

    The message has been logged to the file Errors.txt.

    Here's the code:

    ; ----- Configuration
      #chip mega328p,16
      #option explicit
    
    ; ----- 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 10 s
    
    ; ----- Main
      'for v_i = 1 to 10
        trail = Str(v_a) + Str(v_b) ' this is line 17
    
        HSerPrint trail
        HSerPrintCRLF
    
        'trail = Str(v_a) + Str(v_b) + Str(v_c)
    
    '    HSerPrint trail
    '    HSerPrintCRLF
    '    HSerPrintCRLF
    
      'next v_i
    
      End
    
     
  • stan cartwright

    stan cartwright - 2017-12-09

    it compiles with

     trail = Str((v_a) + (v_b)) ' this is line 17
    
     
  • George Alvarez

    George Alvarez - 2017-12-09

    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:

      'USART settings
      #define USART_BAUD_RATE 9600
      #define USART_TX_BLOCKING
    

    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 * 16
    Dim 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

     
  • stan cartwright

    stan cartwright - 2017-12-10

    . 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
  • George Alvarez

    George Alvarez - 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.

     
  • Anobium

    Anobium - 2017-12-10

    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
    
     
  • George Alvarez

    George Alvarez - 2017-12-10

    Thanks Anobium, that makes perfect sense. Also, I like the new ability to concatenate a variable to itself.

     
    • Anobium

      Anobium - 2017-12-10

      Check the Help.   You can.

       
  • George Alvarez

    George Alvarez - 2017-12-10

    Yes, that's what I meant. I like it!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.