Log In  

Hello.

I am working on an interesting project at the moment yet I need SPLIT() to work correctly for digits and ignore character divisions that are not part of a set. Let me explain.

If you have:

a="12345678"
b=split(a,"3")
c=b[1]
cls()
?c

You get a nice "12" which is correct

If you try:

a="12345678"
b=split(a,"")
c=b[1]
cls()
?c

You get "1" which is interesting cause it means you can separate a string by individual characters this way.

But what about split() for something that is not there ?

HERE IS THE PROBLEM !

a="12345678"
b=split(a,"z")
c=b[1]
cls()
?c

You get this crazy # of 24910 !

Where the heck did that come from ? I can understand it doing the string but there is no "9" even in my pattern !

Comments ?

P#118751 2022-10-07 21:26 ( Edited 2022-10-07 21:36)

1

You want to pass false as the 3rd parameter to avoid automatic conversion of strings to numbers.

P#118752 2022-10-07 21:32

A 3rd statement, @thisismypassword ?

Oh for good grief. ==SIGH==

I didn't know there was a 3rd argument. Closing out this ticket. That was fast. :D Thanks !

I think I need to stop trying to learn code by reading others examples and set down to learn the commands properly from the real instructions and documents.

Still ... I think split() should not return anything if the character is not in there, or at least that would help me:

a="12345678"
b=split(a,"z",false)
c=b[1]
cls()
?c

Returns "12345678" instead of ""

P#118753 2022-10-07 21:36 ( Edited 2022-10-07 21:38)

Well I understand in your particular case an empty string as result would be useful and very welcome... 😉
But I suppose the entire -unsplitted- string is expected from a split function (simply, it failed to split it because there's no such separator, the full string as first and sole token is returned).

P#118758 2022-10-07 22:43

Hi @Heracleum:

It's OK, I just code around it:

function instr(a,b)
local c=split(a,b,false)[1]
  if (#c>=#a or b=="") return 0
  return #c+1
end
P#118767 2022-10-08 02:03

[Please log in to post a comment]