Now that is interesting.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.
Line breaks in strings
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Line breaks in strings
-
- Posts: 204
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: Line breaks in strings
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.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
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;
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);
Re: Line breaks in strings
In my library I have versions with 1, 2, ..., 9 parameters, and SetStr.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);
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.
-
- Posts: 204
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: Line breaks in strings
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: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}};
I got lost somewhere in thereYou 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:
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.Code: Select all
var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Re: Line breaks in strings
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.cfbsoftware wrote:I got lost somewhere in there
Last edited by Robert on Mon Apr 17, 2017 1:54 pm, edited 1 time in total.
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Line breaks in strings
I'd like to emphasize the variable number of parameters comment.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.
Also, with the string approach the rows and columns are obvious.
All bets are off for higher dimensional arrays.
-
- Posts: 204
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: Line breaks in strings
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?DGDanforth wrote: I'd like to emphasize the variable number of parameters comment.
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.
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Line breaks in strings
Chris,cfbsoftware wrote: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?DGDanforth wrote: I'd like to emphasize the variable number of parameters comment.
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.
Variable number of parameters would be sufficient to solve the initialization problem.
-Doug