Count words, in an array... How to?

strKeywords = "keyword1, keyword2, keyword3" arrKW = split(strKeywords, ",")

This part is easy.
But what is the user play the game… and insert other chars!?
“keyword1, keyword2, keyword3 , , , , ,”

How do I still get that part to three keywords?


I now put this code in “KeyDown”, to update the label above the textField. It’s working but really, looks little poor…

You want to know how many words there are in the array?
Ubound(arrKW)

Jakob,

I originally answered this, but after a few rereads of your question I do believe I need some additional information on what you are trying to accomplish. Can you give a more detailed description of how you are trying to store/retrieve your keywords?

Thanks

[quote=168961:@Jakob Krabbe]But what is the user play the game… and insert other chars!?
“keyword1, keyword2, keyword3 , , , , ,”

How do I still get that part to three keywords?[/quote]

You could use a Regex, or simply a series of ReplaceAll, to filter out empty keys before splitting.

strKeywords = ReplaceAll("keyword1, keyword2, keyword3 , ,,,, ,, , ,,",chr(34)+" "+chr(34)) // Remove , , strKeywords = replaceall(strKeywords,chr(34)+chr(34)) // remove ,, arrKW = split(strKeywords, ",")

The RegEx code would be:

dim rx as new RegEx
rx.SearchPattern = ",\\x20*(?=,|$)"
rx.ReplacementPattern = ""
rx.Options.ReplaceAllMatches = true

s = rx.Replace( s )

Haha! Thanks Kem!
Maybe I just go RegEx, just of plain laziness…!
My own experiment failed because “there is more than one item with this name and it’s not clear to which it refers”.
(That’s about the most annoying error message there is… )

[code]
DIM strKeywords as String
strKeywords = ReplaceAll(strVar, chr(34) + " ", chr(34))

DIM i as Integer

FOR i = 0 TO UBound( split(strKeywords, “,”) ,1)
IF inStr(strKeywords , “,”) > 0 THEN System.DebugLog strKeywords ’ ReplaceAll(strKeywords , “,” , “,” )
IF inStr(strKeywords , “,”) > 0 THEN ReplaceAll(strKeywords , “,” , “,” ) << ERROR ON THIS LINE

NEXT

DIM arrWords(-1) AS String
arrWords = split(strKeywords, “,”)

return UBound(arrWords,1) +1[/code]

IF inStr(strKeywords , “,”) > 0 THEN strKeywords = ReplaceAll(strKeywords , “,” , “,” ) // assign the return value


Hmm… silly me!!
Thank you!

However, not quite as pleasant as I expect.
I should have posted this image yesterday… it is clear.
I shall try with the RegEx. If that still fails, then I either use the poor alternative or skip this feature or think of something else.
It’s not a vital part of the project, in the greater perspective. It can wait, so to speak! :slight_smile:

If it were me, I would do

dim arr() as string
dim i as integer

arr = split(strKeywords, ",")
for i = ubound(arr) downto 0
   arr(i) = trim(arr(i))
   if arr(i) = "" then arr.remove(i)
next
wordcount = ubound(arr)

Neat!!
That’s really brilliant!!

Thank you for introducing me to DOWNTO and .remove(i) !!
These were all new to me!

While we’re at it, shuffle has a great potential…! :slight_smile:


You have European working hours in that part of Portland!? :slight_smile:

[quote=169272:@Jakob Krabbe]Neat!!
That’s really brilliant!!

Thank you for introducing me to DOWNTO and .remove(i) !!
These were all new to me!

While we’re at it, shuffle has a great potential…! :slight_smile:


You have European working hours in that part of Portland!? :-)[/quote]

I fail to understand why the solution I posted shortly after your question did not even seem to merit a comment on your part, although it does provide a working solution…

Maybe it was not complex enough for your taste ?

Oh, dear…

strKeywords = ReplaceAll("keyword1, keyword2, keyword3 , ,,,, ,, , ,,",chr(34)+" "+chr(34)) // Remove , , strKeywords = replaceall(strKeywords,chr(34)+chr(34)) // remove ,, arrKW = split(strKeywords, ",")

I’m sure this code is great but for the different purpose!
I tried this code and in this situation it was not selective enough. (Text.Area3 // KeyDown)
If I posted the image earlier then the circumstance would be more obvious! Simply my fault. I asked the wrong question. Again…!

Also, I was AFK and not following the thread…

Whatever. You should really work on your communication.