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:
- The pipe "|" - which, as it's a regex token, has to be escaped, so it becomes "\|" - this is the only string literal included.
- 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).
- 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