I didn't know commas were letters

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: I didn't know commas were letters

Post by FourthWorld » Tue May 20, 2025 4:28 pm

richmond62 wrote:
Tue May 20, 2025 8:03 am
My 'problem' with trueWord is that if I throw 'cheese|smelly' at it it splits that up into 2 words when for my needs I need to treat it as a 2 item thing with '|' as the itemDelimiter.
Specialized data formats unrelated to natural language will need different methods than parsing natural language.

Between word, trueWord, item, and token chunk types, along with everything regex offers, there should be enough options available to parse just about any string data with reasonable efficiency.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Tue May 20, 2025 5:26 pm

Richmond.

What Richard said.

Can you post a couple of examples of what you are trying to achieve? The "cheese|smelly" example seems to me a no-brainer, solved with the itemDelimiter, and never going into the world of words. I bet the tools needed are right there.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Tue May 20, 2025 5:42 pm

Richmond.

I did not read your handlers in your earlier post, but I noticed that nobody else posted one to deal with your initial request. Maybe this will be clearer. It certainly is shorter. :wink: In a button script:

Code: Select all

on mouseup
   get "I like cheese and pickle, I like cheese|pickle, I like sausages|mustard"
   set the itemDel to "|"
   
   repeat for each word tWord in it
      if tWord contains "|" then put word 1 of item 1 of tWord into tWord
      put tWord & space after accum
   end repeat
   answer accum
end mouseup
No regex. You could have just as easily found item 2 instead of item 1

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Tue May 20, 2025 7:15 pm

I did actually work out a way to do what I wanted about a week ago, but I posted it elsewhere. 8)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Tue May 20, 2025 8:33 pm

Richmond.

Really? A week ago? Your first post was less than that ago. What then were you asking about? And why?

And just for grins, how did you solve it? Did it have anything at all to do with "commas being letters"? Did it have anything at all, in fact, to do with anything at all?

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Tue May 20, 2025 9:40 pm

Thee'll wait until the morn's morn for that as I'm abed betimes. 8)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Wed May 21, 2025 7:49 am

This is how I got what I wanted: and about as crude and as simple as one can get:

Code: Select all

on mouseup
   put empty into fld "f2"
   put the number of words in fld "f1" into NDICK
   put 1 into KOUNT
   repeat until KOUNT > NDICK
      if word KOUNT of fld "f1" contains "|" then
         put word KOUNT of fld "f1" into DIX
         set the itemDelimiter to "|"
         put item 1 of DIX into DUX
         if item 2 of DIX contains "," then
            put "," after DUX
         end if
         put " " & DUX after fld "f2"
      else
         put " " & word KOUNT of fld "f1" after fld "f2"
      end if
      add 1 to KOUNT
      end repeat
   end mouseup
This solution was posted 'elsewhere' on 14 May 2025.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Wed May 21, 2025 2:13 pm

Richmond.

OK.

But then why this thread?

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Wed May 21, 2025 3:46 pm

But then why this thread?
Because I jumped before I was pushed. :D

Actually because my first, slightly knee-jerk reaction was to post a question in the hope that something could be done slightly more 'automagically' than the way I EVENTUALLY worked out.

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: I didn't know commas were letters

Post by stam » Wed May 21, 2025 4:07 pm

richmond62 wrote:
Wed May 21, 2025 3:46 pm
Actually because my first, slightly knee-jerk reaction was to post a question in the hope that something could be done slightly more 'automagically' than the way I EVENTUALLY worked out.
For 'automagically' resolving this, use regex. The below reduces your 17-line command above to a 1-line command:

Code: Select all

On mouseUp
   put replaceText(field "f1", "\|\w*", "") into field "f2"
end mouseUp

I had posted this suggestion on page 1 of this disproportionately lengthy discussion, and you complained this could not process the word flowing the pipe symbol, but as far as I can see (and please do correct me if I'm wrong), you are in any case not processing the word following the pipe, so this 1-liner does exactly the same thing...

Not to mention that this 1-liner will also catch cases your code won't, eg word1|word2 followed by a "." or a ";" any syntax that isn't a word, instead of just a comma.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Thu May 22, 2025 8:36 am

"Disproportionately long" . . . judgemental as ever.

In proportion to what? To your subjective decision on how long something should be.

I have a load of 9 & 10 years coming up: I am NOT going to get into regex with them.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Thu May 22, 2025 2:38 pm

Richmond.

I think Stam was referring to the rather inchoate underpinnings of your thread. It was difficult to maintain a linear and coherent discussion, there was no clear development of the topic, which made little sense from the getgo, and even the title was misleading.

And it turns out that you had already written a handler that solved your problem before you posted, making the considerable effort others put into this thread essentially a waste of time.

As I said earlier, I know you are a serious person. I know you enjoy LC. You are third in the panoply of users, at least based on the number of posts.

You must admit that Stam has a point.

So don't change who you are or what you do, but do try to be more focussed.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: I didn't know commas were letters

Post by richmond62 » Thu May 22, 2025 4:20 pm

I worked out a handler about 24 hours after my first post.

And re logic and coherence, as I was not exactly sure HOW, no things were not that logical . . .

My solution meant I reverted to my 'spade and shovel' method, when I had hoped for something more elegant which the 9-10 year old crowd could also understand.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: I didn't know commas were letters

Post by dunbarx » Thu May 22, 2025 4:46 pm

Richmond.

OK. I am old-fashioned as well. But that is a matter of style, not substance, of the public effort here.

You see that the three-page thread could have been 1/3 page.

Craig

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: I didn't know commas were letters

Post by stam » Fri May 23, 2025 4:13 am

richmond62 wrote:
Thu May 22, 2025 8:36 am
I have a load of 9 & 10 years coming up: I am NOT going to get into regex with them.
Pretty sure this never came up as a limitation to possible solutions until now. And why wouldn't you want to teach young ones this extremely portable and key skill, that can be used with any modern language?

Accepting the implied premise that it may be difficult for younger people to understand abstract concepts, it is quite possibly doable to teach at least a tiny subset of regex, even to younger people. For example, in the regex I include above there are only 3 components:
  1. The pipe "|" - which, as it's a regex token, has to be escaped, so it becomes "\|" - this is the only string literal included.
  2. Then you have \w, which means "word char" - a char that could be part of a word (so it excludes, commas, full stops, white space, etc).
  3. Then you have the *, which means zero or more of the immediate previous item, in this case \w
So this identifies a string that begins with a a pipe, followed by a single word of any length. Is this really beyond the ability of younger minds and is it really that much less comprehensible than your 17 line handler?

As for your solution, it works, but in narrow confines.
For example, if word1|word2 is followed by a ":" or a ";" instead of a comma, it will fail - and they you'd have to create even more complex code to try and address each fringe case. The code becomes significantly more complex and more difficult to understand and maintain - and teach. Also remembering that repeat with x loops are slower with long texts.


I know I still recommended regex to you above, but we all know, not only from this thread, that you will never actually try to learn or use regex, regardless of whether you are teaching 9 year olds or not (from your comments outside of this thread, this seems to be some kind of "code purity" issue for you)
So I won't waste my electronic breath trying to convince you. Here is an alternative solution is about half the length of your solution and which won't fail if word1|word2 ends with a colon, semicolon, etc instead of comma:

Code: Select all

function deletePipedWord pSource
    local tChar, tNonWordChars = ",.;:/\" -- can be changed as needed; assumes that each of these is followed by a space 
    set the itemDelimiter to  "|"
    repeat for each word tWord in pSource
        if tWord contains "|" then
            if the last char of tWord is in tNonWordChars then delete the last char of tWord
            replace tWord with item 1 of tWord in pSource 
        end if
    end repeat
    return pSource
end deletePipedWord

Code: Select all

on mouseUp
    put deletePipedWord(field 1) into field 2
end mouseUp

Post Reply