Page 1 of 2
How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 10:39 pm
by DavJans
There has to be a better way than this?
Code: Select all
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
replace " " with "," in tLine
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:25 pm
by dunbarx
With your data in a field 1:
Code: Select all
on mouseUp
get fld 1
replace " " with "" in it
replace space with comma in it
answer it
end mouseUp
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:36 pm
by dunbarx
Hmmm.
Always too quick. My handler above fails if pairs of TWO spaces are present in the text.
Anyway, again, you have to use the tools LC offers:
Code: Select all
on mouseUp
get fld 1
set the itemDel to space
repeat with y = 1 to the number of lines of it
put item 1 of line y of it & "," & last item of Line y of it & return after accum
end repeat
answer accum
end mouseUp
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:44 pm
by dunbarx
Me again.
It bugged me that using "replace", which is so powerful and cool, was having issues with mixed lengths of spaces. Not surprising, I guess.
Anyway, yet again, this works:
Code: Select all
on mouseUp
get fld 1
repeat until " " is not in it
replace " " with " " in it
end repeat
repeat until " " is not in it
replace " " with "," in it
end repeat
replace space with comma in it
answer it
end mouseUp
Now the job is to do it all with one repeat loop, or better, with none.
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:47 pm
by DavJans
Nice thank you, that looks much cleaner and efficient.
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:52 pm
by dunbarx
Good.
Which one?
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Thu May 12, 2022 11:56 pm
by dunbarx
I am embarrassed to have used "repeat with" here when this will be so much faster.
Code: Select all
on mouseUp
get fld 1
set the itemDel to space
repeat for each line tLine in it
put item 1 of tline & "," & last item of tLine & return after accum
end repeat
answer accum
end mouseUp
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 5:45 am
by dunbarx
I do go off on extravagant journeys into the weeds.
It is spaces that are the culprits here. So why go to all the trouble of setting the itemDelimiter to "space" when spaces already and intrinsically delimit words.
In other, er , words:
Code: Select all
on mouseUp
get fld 1
repeat for each line tLine in it
put word 1 of tline & "," & last word of tLine & return after accum
end repeat
answer accum
end mouseUp
It was not all in vain, I suppose. If you had anything other than strings of spaces as those pesky interior characters between the endpoints, then the item method would be universal,
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 10:44 am
by stam
DavJans wrote: ↑Thu May 12, 2022 10:39 pm
There has to be a better way than this?
Yes there is. In fact many, as you can see from the posts above.
I personally prefer to use the best tool for the job, which for me means a) less code but still understandable and b) more efficient.
So for me (and i know others disagree on this because of a desire for 'livecode purity'), this is a job for
regex - in particular with
replaceText() which takes a regular expression as a search string. So you can do this in
one line of code:
Code: Select all
put replaceText(field 1, "\s+", comma) into field 1
If field 1 contains:
this..is.a...test.....for.......multiple..........spaces (unhelpfully, the forum converts multiple spaces into 1 space lol! so i've substituted these with a '.' for illustration purposes

), running this code will put
this,is,a,test,for,multiple,spaces into field 1
HTH,
Stam
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 2:07 pm
by dunbarx
Stam.
I knew someone just like you would raise the regex thing.
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 3:57 pm
by stam
It’s just another way to achieve the same goal… no right or wrong really, as long as job gets done.
For me, it’s the best tool for the job… yes I *could* write a loop, but why not use all the tools at my disposal and select the on that fits best… if code can be executed simply in one line instead of a multiline loop (and I avoid loops wherever possible) it’s a clear winner for me.
The regex is simple -
\s is the token equivalent to the keyword
space in LiveCode and the
+ just says one or more of these. So it will replace each group of one or more spaces with a comma. Very easy and simple

Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 4:56 pm
by jacque
The right tool for the right job.

I would use regex too. It's still LC because we have a function built in for it.
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 5:26 pm
by richmond62
Purism is like most things, alright up to a point.
And while regex may not be 'pure' LiveCode, it is certainly purer than fudging around with Java script from inside LiveCode.
And it doesn't seem much worse than using an image editor to get the images one intends to use inwith LiveCode 'just right'.
I would use the repeat loop ONLY because I haven't taken the trouble to learn regex.

Re: How can I replace a random amount of spaces with 1 comma?
Posted: Fri May 13, 2022 11:28 pm
by dunbarx
I have not really gotten much acquainted with regex either, though I have used it here and there in simple scenarios.
I am just old fashioned, and immediately tried to make "replace" work. When it really did not, I used another "pure" LC gadget, "item", and then simplified to "word".
A very long time ago, in HC, Colin Holgate and I used to try to outdo each other by reducing a coding task to the minimum number of lines. He usually won. The point is that we dealt with the "pure" tools available.
Regex is available, mainstream, and not in any way exotic. Its compactness, seems un-LC-like. But it holds enormous power and, er, compactness.
I am likely to remain lazy...
Craig
Re: How can I replace a random amount of spaces with 1 comma?
Posted: Sat May 14, 2022 10:34 am
by richmond62
So, now might be the appropriate moment to ask Jacque the best place to go to learn regex.
The Whackypedia article, interesting as most whackpedia articles tend to be, just made what my dear, dead Dad called "my prawns" shrivel .
