How can I replace a random amount of spaces with 1 comma?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

DavJans
Posts: 263
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

How can I replace a random amount of spaces with 1 comma?

Post by DavJans » Thu May 12, 2022 10:39 pm

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
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Thu May 12, 2022 11:25 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Thu May 12, 2022 11:36 pm

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
Last edited by dunbarx on Thu May 12, 2022 11:47 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Thu May 12, 2022 11:44 pm

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
Last edited by dunbarx on Thu May 12, 2022 11:54 pm, edited 1 time in total.

DavJans
Posts: 263
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: How can I replace a random amount of spaces with 1 comma?

Post by DavJans » Thu May 12, 2022 11:47 pm

Nice thank you, that looks much cleaner and efficient.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Thu May 12, 2022 11:52 pm

Good.

Which one?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Thu May 12, 2022 11:56 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Fri May 13, 2022 5:45 am

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

stam
Posts: 1201
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How can I replace a random amount of spaces with 1 comma?

Post by stam » Fri May 13, 2022 10:44 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Fri May 13, 2022 2:07 pm

Stam.

I knew someone just like you would raise the regex thing.

Craig

stam
Posts: 1201
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How can I replace a random amount of spaces with 1 comma?

Post by stam » Fri May 13, 2022 3:57 pm

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 ;)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6544
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How can I replace a random amount of spaces with 1 comma?

Post by jacque » Fri May 13, 2022 4:56 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7226
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: How can I replace a random amount of spaces with 1 comma?

Post by richmond62 » Fri May 13, 2022 5:26 pm

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. :|

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How can I replace a random amount of spaces with 1 comma?

Post by dunbarx » Fri May 13, 2022 11:28 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7226
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: How can I replace a random amount of spaces with 1 comma?

Post by richmond62 » Sat May 14, 2022 10:34 am

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 . :|

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”