Four Js Development Tools Forum

General => Ask Reuben => Topic started by: Reuben B. on April 14, 2020, 02:18:06 am



Title: Ask Reuben 3 - Append (,) or Concatenate (||)
Post by: Reuben B. on April 14, 2020, 02:18:06 am
What should I use to join two or more strings together? Append (,) or Concatenate (||) ?

A question I have been asked a few times, is it better to use append (,) or concatenate (||) to join two strings together?

The quick answer is what result do you expect if one of the operands is NULL.  With concatenation (||), if one of the operands is NULL then the result will be NULL as well.  So for a quick test, note difference in output of ...

However the answer I like to give back is that are you aware that there is a 3rd option?, that is the SFMT operator.  IMHO this leads to code that is easier to read.

Read more at https://4js.com/ask-reuben/ig-3/


Title: Re: Ask Reuben 3 - Append (,) or Concatenate (||)
Post by: Rene S. on April 14, 2020, 12:28:35 pm
Ask different:
Why do two distinct forms of string concatenation exist? 1st: ',' and 2nd: '||'

Answer:
The first form of string concatenation is a statement:
LET string-variable = expr ( ',' expr )...
Read as: "LET string-variable = comma separated expression list"

The second form of string concatenation is an expression:
This is a fragment of the expression grammar:
expr := expr '||' expr
The concatenation result can directly be used wherever 4gl accepts expressions (example: function parameters).

Another remark:
The first form of the string concatenation produces a formatted string: numeric, date, date-time, interval and char() operands will be formatted with a fix width. See the print statement of reports. The first form of the string concatenation has the same semantics as the print statement.


Title: Re: Ask Reuben 3 - Append (,) or Concatenate (||)
Post by: Leo S. on April 14, 2020, 12:55:04 pm
Like Reuben I personally avoid the concat operator due to the NULL semantics.
If one still does, use the NVL() operator to avoid the NULL pitfall.
Code
  1. CONSTANT c="hello"
  2. MAIN
  3.  DEFINE a STRING
  4.  CALL echo(c||":"||NVL(a,"a is NULL"))
  5. END MAIN
  6.  
  7. FUNCTION echo(s STRING)
  8.  DISPLAY s
  9. END FUNCTION
  10.