QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: paranoia1001001 on April 02, 2021, 10:21:32 am

Title: Concatenation function
Post by: paranoia1001001 on April 02, 2021, 10:21:32 am
Please correct my memory if I am wrong, but wasn't there a concatenate() or concat() function in QBasic? I've been unable to find any information on the QB64 Wiki about any such function in QB64 or QBasic. Am I just remembering another language from long ago?
Title: Re: Concatenation function
Post by: SpriggsySpriggs on April 02, 2021, 10:23:19 am
You must be remembering a different language. Concatenation is done simply using the "+" operator.

For instance

Code: QB64: [Select]
  1. Print "This " + "is " + "my " + "string"
Title: Re: Concatenation function
Post by: paranoia1001001 on April 02, 2021, 10:37:20 am
You must be remembering a different language. Concatenation is done simply using the "+" operator.

For instance

Code: QB64: [Select]
  1. Print "This " + "is " + "my " + "string"

Thank you very much for that confirmation of officially getting old enough to forget something as basic as BASIC. I feel like I've even used it in VBA within Excel, so maybe it just exists in VBA/VB.

If you'll indulge me a moment longer, from what I read from the WIki about concatenation using the "+" operator, it works on string literals, but makes no mention of string variables. Do you happen to know if it works for them as well? Thank you for any information you have. I would simply do a test to find out, but as you know my install isn't working at the moment. I am heading over now to read your response to that post. Thank you, again, and, in advance!
Title: Re: Concatenation function
Post by: SpriggsySpriggs on April 02, 2021, 10:56:23 am
@paranoia1001001

Yes, you can use the "+" with string variables.
From the Wiki page on STRING:
Quote
Concatenation uses the + addition symbol to add literal or variable parts to a string variable value.
Title: Re: Concatenation function
Post by: luke on April 02, 2021, 10:57:53 am
It works on string expressions, which includes string literals, string variables, and functions that return strings (e.g. CHR$(13) + CHR$(10)).
Title: Re: Concatenation function
Post by: paranoia1001001 on April 02, 2021, 11:31:31 am
@paranoia1001001

Yes, you can use the "+" with string variables.
From the Wiki page on STRING:

Excellent. Thank you very much!
Title: Re: Concatenation function
Post by: paranoia1001001 on April 02, 2021, 11:32:35 am
It works on string expressions, which includes string literals, string variables, and functions that return strings (e.g. CHR$(13) + CHR$(10)).

Very helpful information. Your help is kindly appreciated. I obviously need to start reading more carefully, or not at 3 am.
Title: Re: Concatenation function
Post by: xra7en on April 02, 2021, 01:44:52 pm
Please correct my memory if I am wrong, but wasn't there a concatenate() or concat() function in QBasic? I've been unable to find any information on the QB64 Wiki about any such function in QB64 or QBasic. Am I just remembering another language from long ago?

prob javascript

Code: Javascript: [Select]
  1. var str1 = "I Love";
  2. var str2 = "QB64!";
  3. var res = str1.concat(str2);
Title: Re: Concatenation function
Post by: bplus on April 02, 2021, 01:49:31 pm
At moment, I can't think of any other way to Concatenate strings in QB64?

What am I missing?
Title: Re: Concatenation function
Post by: FellippeHeitor on April 02, 2021, 02:02:03 pm
If you wanna go fancy, cryptic and do unnecessary work, there's MID$:

Code: QB64: [Select]
  1. a$ = "Hello, "
  2. b$ = "World!"
  3. result$ = concat$(a$, b$)
  4. Print result$
  5.  
  6. Function concat$ (str1$, str2$)
  7.     Dim temp$
  8.     temp$ = Space$(Len(str1$) + Len(str2$))
  9.     Mid$(temp$, 1) = str1$
  10.     Mid$(temp$, Len(str1$) + 1) = str2$
  11.     concat$ = temp$
Title: Re: Concatenation function
Post by: bplus on April 02, 2021, 02:05:06 pm
You beat me! same exact thing!

Here is one I just invented:
Code: QB64: [Select]
  1. Print concatenate$("Test", " this.")
  2. Function concatenate$ (a$, b$)
  3.     c$ = Space$(Len(a$) + Len(b$))
  4.     Mid$(c$, 1, Len(a$)) = a$
  5.     Mid$(c$, Len(a$) + 1, Len(b$)) = b$
  6.     concatenate$ = c$
  7.  

All +'s used in the creation of this Function are for numeric adding and not concatenation.
Title: Re: Concatenation function
Post by: paranoia1001001 on April 02, 2021, 04:51:51 pm
There are obviously some smart people here. OK, if you want a challenge the function (from whatever language it came, thinking VB right now but not sure) was able to take any number of strings as arguments and concatenate them, similar to our very simple tacking on of "+"'s ad infinitum. Have at it!
Title: Re: Concatenation function
Post by: SpriggsySpriggs on April 02, 2021, 04:58:11 pm
Code: QB64: [Select]
  1.  
  2. Dim As String testarray(1 To 3)
  3.  
  4. testarray(1) = "This is"
  5. testarray(2) = " a "
  6. testarray(3) = "test"
  7.  
  8. Print concat(testarray())
  9.  
  10. Function concat$ (stringarray() As String)
  11.     Dim As String concatenated
  12.     For x = LBound(stringarray) To UBound(stringarray)
  13.         concatenated = concatenated + stringarray(x)
  14.     Next
  15.     concat = concatenated
Title: Re: Concatenation function
Post by: bplus on April 02, 2021, 05:06:17 pm
Code: QB64: [Select]
  1.  
  2. Dim As String testarray(1 To 3)
  3.  
  4. testarray(1) = "This is"
  5. testarray(2) = " a "
  6. testarray(3) = "test"
  7.  
  8. Print concat(testarray())
  9.  
  10. Function concat$ (stringarray() As String)
  11.     Dim As String concatenated
  12.     For x = LBound(stringarray) To UBound(stringarray)
  13.         concatenated = concatenated + stringarray(x)
  14.     Next
  15.     concat = concatenated

Hey @SpriggsySpriggs what are you doing in line 15 of your code?  ;-))

You should consider your overload example.
Title: Re: Concatenation function
Post by: SpriggsySpriggs on April 02, 2021, 05:11:32 pm
@bplus Nah.
Title: Re: Concatenation function
Post by: bplus on April 02, 2021, 05:26:35 pm
There are obviously some smart people here. OK, if you want a challenge the function (from whatever language it came, thinking VB right now but not sure) was able to take any number of strings as arguments and concatenate them, similar to our very simple tacking on of "+"'s ad infinitum. Have at it!

Code: QB64: [Select]
  1. Dim As String testarray(1 To 12)
  2.  
  3. testarray(1) = "This is"
  4. testarray(2) = " a "
  5. testarray(3) = "test"
  6. testarray(4) = " to"
  7. testarray(5) = " show"
  8. testarray(6) = " Spriggsy"
  9. testarray(7) = " had"
  10. testarray(8) = " part"
  11. testarray(9) = " of"
  12. testarray(10) = " solution"
  13. testarray(11) = " correct"
  14. testarray(12) = "."
  15.  
  16.  
  17. Print polycat$(testarray())
  18.  
  19. Function polycat$ (arr$())
  20.     c$ = arr$(LBound(arr$))
  21.     For i = LBound(arr$) + 1 To UBound(arr$)
  22.         c$ = concatenate$(c$, arr$(i))
  23.     Next
  24.     polycat$ = c$
  25.  
  26. Function concatenate$ (a$, b$)
  27.     c$ = Space$(Len(a$) + Len(b$))
  28.     Mid$(c$, 1, Len(a$)) = a$
  29.     Mid$(c$, Len(a$) + 1, Len(b$)) = b$
  30.     concatenate$ = c$
  31.  
  32.  
  33.  
Title: Re: Concatenation function
Post by: SMcNeill on April 02, 2021, 06:40:37 pm
FUNCTION Concat$(s1$, s2$)
   l = LEN(s1$) + len(s2$)
   DIM m AS _MEM: m = _MEMNEW(l)
   _MEMPUT m, m.OFFSET, s1$
   _MEMPUT m, m.OFFSET + len(s1$), s2$
   Concat$ = SPACE$(l)
   _MEMGET m, m.OFFSET, Concat$
   _MEMFREE m
END FUNCTION

If we’re looking for obscure ways to just add strings together, you can always do the above...
Title: Re: Concatenation function
Post by: TempodiBasic on April 03, 2021, 05:13:08 am
Hi guys and gals
@paranoia1001001
about what language has concat as native function...
I remember
Pascal both TurboPascal/ Borland Pascal both FreePascal have concat  keyword
see here
Quote
Concat : Concatenates two or more strings
Syntax : concat(s1,s2,...,sn)
Example : st:=concat(s1,s2);
If s1='ABC' and s2='DEF', st would be 'ABCDEF'
st:=concat('Borland ','Pascal ','ver. ','7.0'); Would be 'Borland Pascal ver. 7.0'
You may put as many parameters to concat as possible. If the resulting string length is more than 255, it will be truncated to 255.
Concat is the same if we use plus sign (+). For example :
st:=concat('ABC','DEF'); is the same as st:='ABC'+'DEF'
http://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/pascal/PasHTM1/pas/pasl1007.html (http://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/pascal/PasHTM1/pas/pasl1007.html)

but in the same Pascal you can use + as in BASIC
Quote
Function Concat(s1 [, s2, s3...sn] : String) : String;

Description

Concatenates 2 or more strings depending how long is the argument expression. Try to make sure not to exceed the limit of 255 characters when concatening strings as it will result in truncation. This function can also be obtained by using the plus (+) operator between strings that need to be concatenated.

Var
   S1, S2 : String;

Begin
   S1 := 'Hey!';
   S2 := ' How are you?';
   Write(Concat(S1, S2)); { 'Hey! How are you?' }
End.
is the same
Var
   S1, S2 : String;

Begin
   S1 := 'Hey!';
   S2 := ' How are you?';
   Write(S1 + S2); { 'Hey! How are you?' }
End.
https://www.pascal-programming.info/lesson10.php#jump7 (https://www.pascal-programming.info/lesson10.php#jump7)
https://www.freepascal.org/docs-html/rtl/system/stringfunctions.html (https://www.freepascal.org/docs-html/rtl/system/stringfunctions.html)

Thanks to read
Title: Re: Concatenation function
Post by: xra7en on April 03, 2021, 10:29:49 pm
does this work for unnecessary  fun?

Code: QB64: [Select]
  1. s(1) = "I": s(2) = "love": s(3) = "qb64"
  2. 'goofy obnoxious cryptic long ver
  3. Print concat(s())
  4.  
  5.  
  6. Function concat$ (s() As String)
  7.         Dim cc As String
  8.         Dim i, j As Integer
  9.         Dim cnt As Integer
  10.         cc = "": i = 1: Do
  11.                 For j = 1 To Len(s(i)): cc = cc + Chr$(Asc(Mid$(s(i), j, 1))): Next
  12.                 cc = cc + Chr$(&H20)
  13.                 i = i + 1
  14.         Loop Until i > UBound(s)
  15.         concat$ = cc
Title: Re: Concatenation function
Post by: bplus on April 04, 2021, 11:35:40 am
Fun? or obsession? LOL

Code: QB64: [Select]
  1. Dim As String testarray(1 To 16)
  2.  
  3. testarray(1) = "Well"
  4. testarray(2) = " hell"
  5. testarray(3) = "!"
  6. testarray(4) = " I"
  7. testarray(5) = " can't"
  8. testarray(6) = " stop"
  9. testarray(7) = " trying"
  10. testarray(8) = " new"
  11. testarray(9) = " ways"
  12. testarray(10) = " to"
  13. testarray(11) = " bypass"
  14. testarray(12) = " using"
  15. testarray(13) = " +"
  16. testarray(14) = " for"
  17. testarray(15) = " concatenation"
  18. testarray(16) = "."
  19.  
  20. Print concatArr$(testarray())
  21.  
  22. Function concatArr$ (arr$())
  23.     Open "temp.txt" For Output As #1
  24.     For i = LBound(arr$) To UBound(arr$)
  25.         Print #1, arr$(i);
  26.     Next
  27.     Close #1
  28.     Open "temp.txt" For Input As #1
  29.     Line Input #1, concatArr$
  30.     Close #1
  31.     Kill "temp.txt"
  32.  
  33.  
  34.