Line breaks in strings

User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Line breaks in strings

Post by DGDanforth »

Robert wrote: Incidently, if you use a TextField Control to input a string it has a Multi Line option that allows you to directly input line breaks.
Now that is interesting.
cfbsoftware
Posts: 204
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Line breaks in strings

Post by cfbsoftware »

DGDanforth wrote: OK that's a good point.
But we have fudged string processing by restricting strings to not contain line breaks simply so forgetful errors of not closing a string doesn't cause the catastrophe you describe. That feels like an "engineering" solution to a theory. What is a "string"?
-Doug
That is just one reason. The indentation problem that Josef mentioned is another one. A string is a sequence of zero or more characters. "What do we want to use strings for?" is the question that should be asked.

The example that triggered this discussion is a good example of presenting a solution and pointing out the limitations of that solution rather than describing the problem. My understanding of the problem is that it is required to populate an integer matrix with a fixed set of values. Is that correct? If so, I would not use strings to do this. One simple solution could be to create a procedure that populates a single row:

Code: Select all

PROCEDURE SetRow(VAR m: MatRow; c0, c1, c2, c3, c4, c5: INTEGER);
BEGIN
  m[0] := c0; m[1] := c1; m[2] := c2; m[3] := c3; m[4] := c4; m[5] := c5
END SetMat;
The matrix is then populated by calling that procedure once for each row:

Code: Select all

SetRow(mat[0], 1, 1, 1, 0, 0, 0);
SetRow(mat[1], 1, 0, 1, 0, 0, 0);
SetRow(mat[2], 1, 1, 1, 0, 0, 0);
SetRow(mat[3], 0, 0, 1, 1, 1, 0);
SetRow(mat[4], 0, 0, 1, 1, 0, 0);
SetRow(mat[5], 0, 0, 1, 1, 1, 0);
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Line breaks in strings

Post by Robert »

cfbsoftware wrote:One simple solution could be to create a procedure that populates a single row:

Code: Select all

PROCEDURE SetRow(VAR m: MatRow; c0, c1, c2, c3, c4, c5: INTEGER);
In my library I have versions with 1, 2, ..., 9 parameters, and SetStr.
The advantage of the (horribly inefficient) SetStr approach is that it holds a variable number of parameters, and it is easy to change the number of parameters without changing the procedure name as well.
Both approaches have their place.
cfbsoftware
Posts: 204
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Line breaks in strings

Post by cfbsoftware »

Robert wrote: would this work?

Code: Select all

int train_X[6][6] = {{1, 1, 1, 0, 0, 0},{1, 0, 1, 0, 0, 0},{1, 1, 1, 0, 0, 0},{0, 0, 1, 1, 1, 0},{0, 0, 1, 0, 1, 0},{0, 0, 1, 1, 1, 0}};
You can do this sort of array initialisation in other languages like Delphi, C#, Swift etc. However, I always find it difficult to remember which are the rows, columns etc. once it gets beyond simple examples. This is from Apple's Swift documentation:
You can create multidimensional arrays by nesting pairs of square brackets, where the name of the base type of the elements is contained in the innermost pair of square brackets. For example, you can create a three-dimensional array of integers using three sets of square brackets:

Code: Select all

var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
When accessing the elements in a multidimensional array, the left-most subscript index refers to the element at that index in the outermost array. The next subscript index to the right refers to the element at that index in the array that’s nested one level in. And so on. This means that in the example above, array3D[0] refers to [[1, 2], [3, 4]], array3D[0][1] refers to [3, 4], and array3D[0][1][1] refers to the value 4.
I got lost somewhere in there :?
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Line breaks in strings

Post by Robert »

cfbsoftware wrote:I got lost somewhere in there :?
There is a reason why I've never bothered to go beyond 1-dimension in the past, and am not looking for an arbitrary dimensional capability today.
Last edited by Robert on Mon Apr 17, 2017 1:54 pm, edited 1 time in total.
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Line breaks in strings

Post by DGDanforth »

Robert wrote:The advantage of the (horribly inefficient) SetStr approach is that it holds a variable number of parameters, and it is easy to change the number of parameters without changing the procedure name as well.
Both approaches have their place.
I'd like to emphasize the variable number of parameters comment.

Also, with the string approach the rows and columns are obvious.
All bets are off for higher dimensional arrays.
cfbsoftware
Posts: 204
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Line breaks in strings

Post by cfbsoftware »

DGDanforth wrote: I'd like to emphasize the variable number of parameters comment.
If you had a way in CP of specifying a variable number of parameters to a procedure would you have still thought that you needed embedded line-breaks in strings?

Variable numbers of parameters can have their advantages. Unfortunately, the way they are usually implemented undermines safety checks (e.g. forgetting to supply a parameter when one is required) which can lead to obscure and hard to find bugs.
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Line breaks in strings

Post by DGDanforth »

cfbsoftware wrote:
DGDanforth wrote: I'd like to emphasize the variable number of parameters comment.
If you had a way in CP of specifying a variable number of parameters to a procedure would you have still thought that you needed embedded line-breaks in strings?

Variable numbers of parameters can have their advantages. Unfortunately, the way they are usually implemented undermines safety checks (e.g. forgetting to supply a parameter when one is required) which can lead to obscure and hard to find bugs.
Chris,
Variable number of parameters would be sufficient to solve the initialization problem.
-Doug
Post Reply