jtokach
(Seasoned Scripter)
2005-06-23 12:03 AM
Join - Split syntax

I recall seeing some pretty nifty Join(Split... code in a few scripts. I'd like to see them documented somewhere, b/c I could never seem to find them when I need it nor am I completely straight on what the limitations of them are.

For instance, can I reference/return the ubound element of split'd array in one short line of code?

Something like this: Code:
 Split($MyString,",")[Ubound(Split($MyString,","))] 



LonkeroAdministrator
(KiX Master Guru)
2005-06-23 12:17 AM
Re: Join - Split syntax

yes, you can do that.
but it's thought to be resource hog and thus almost never used.
with small string, it would not at all but with larger data and huge loops, it counts for lots of seconds.


jtokach
(Seasoned Scripter)
2005-06-23 12:28 AM
Re: Join - Split syntax

Post some code man! Any join/split tricks you've got.(You've only 92 to go.) I can see where the double split would kill performance in a massive loop.

LonkeroAdministrator
(KiX Master Guru)
2005-06-23 12:49 AM
Re: Join - Split syntax

there is no tricks really...
join is simple thing, so is split.
can't understand what you mean with tricks.
and really, I only have 90...


Richard H.Administrator
(KiX Supporter)
2005-06-24 09:54 AM
Re: Join - Split syntax

Hmmm... Join / Split tricks...

Ok, here's one that I use quite often:
Code:
While InStr($s,$c+$c) $s=Join(Split($s,$c+$c),$c) Loop


Useful, eh?


Sealeopard
(KiX Master)
2005-06-24 01:40 PM
Re: Join - Split syntax

Nice way to remove dupes in a string.

jtokach
(Seasoned Scripter)
2005-06-24 05:22 PM
Re: Join - Split syntax

Quote:

Hmmm... Join / Split tricks...

Ok, here's one that I use quite often:
Code:
While InStr($s,$c+$c) $s=Join(Split($s,$c+$c),$c) Loop


Useful, eh?




Richard, can you post some example code?


AllenAdministrator
(KiX Supporter)
2005-06-24 07:36 PM
Re: Join - Split syntax

These UDFs take advantage of some of the split join techniques...

SNVerify()
Replace()

Any help?


LonkeroAdministrator
(KiX Master Guru)
2005-06-24 09:56 PM
Re: Join - Split syntax

jim, is there actually what you are after?
richie had a clean general example and you ask for example after that...


jtokach
(Seasoned Scripter)
2005-06-24 11:10 PM
Re: Join - Split syntax

Jooel, yes this is the kind of stuff I was hoping for. I asked for an example of how to use the code.

LonkeroAdministrator
(KiX Master Guru)
2005-06-24 11:32 PM
Re: Join - Split syntax

well, place any string in $s and any character in $c and it will remove the dublicates.

Richard H.Administrator
(KiX Supporter)
2005-06-27 12:51 PM
Re: Join - Split syntax

Quote:

Richard, can you post some example code?




It's really as simple as Jooel states. You would use it to remove redundant characters from strings such as spaces or blank lines in file text.

It's so useful that I have it as a UDF.
Code:
; udfTRIM()
;
; Remove repeated characters from string, also removes leading and trailing
; characters.
;
Function udfTrim($s,$c)
While InStr($s,$c+$c) $s=Join(Split($s,$c+$c),$c) Loop
If Left($s,1)=$c $s=SubStr($s,2) EndIf
If Right($s,1)=$c $s=Left($s,-1) EndIf
$udfTrim=$s
EndFunction



A couple of examples
  1. $s=udfTrim($s," ") ; remove redundant spaces from string
  2. $sFileData=udfTrim($sFileData,@CRLF) ; remove blank lines from data


[edit]Oops - removed the leading/trailing stuff as it didn't work for all cases.[/edit]

[edit]Updated leading/trailing stuff - not as pretty as original but it works.[/edit]


jtokach
(Seasoned Scripter)
2005-06-27 07:18 PM
Re: Join - Split syntax

Thanks for taking the time to post some examples. This is what I mean by knowing the limitations. Sometimes it's just not that obvious such as with second example. That's freakin' awesome!

NTDOCAdministrator
(KiX Master)
2005-06-27 07:35 PM
Re: Join - Split syntax

From PostPrep

Code:
  ; $f contains all file text. Replace characters "&", "<", and ">" with non-printing
; characters, then split into an array of LINES. Hiding the replaced chars prevents
; treating them during the HTML processing
 
$f=split(join(split(join(split(join(split($f,"&"),$1),"<"),$2),">"),$3),@crlf)



 


jtokach
(Seasoned Scripter)
2005-06-27 07:48 PM
Re: Join - Split syntax

OT: Curios Doc, is this where $1 = chr(38) $2 = chr(60) ...

Otherwise, an interesting implementation. I still can't believe I never thought to split on @CRLF. I wrote a stupid udf to do file2array().


AllenAdministrator
(KiX Supporter)
2005-06-28 01:32 AM
Re: Join - Split syntax

In another thread , I ran into another limitation which was splitting on chr(###) when ### is greater than 127. If you place the actual character in the split, it splits fine, but not with its chr() counterpart.

LonkeroAdministrator
(KiX Master Guru)
2005-06-28 02:05 AM
Re: Join - Split syntax

allen, actually...
did you investigate the reason for that?
like, did you have ascii set to on or something?
chr() should split fine. if not, report it.


AllenAdministrator
(KiX Supporter)
2005-06-28 02:49 AM
Re: Join - Split syntax

to quote a wise contributer around here....

"hmmm..."


LonkeroAdministrator
(KiX Master Guru)
2005-06-28 03:14 AM
Re: Join - Split syntax

well, did a quick test and no, not a single one of them fail.
but, they kinda do.
if you don't set casesensitivity, kixtart uses double 7 bit chartable, kinda...
well, see for yourself:
Code:

;fill a string with all chars...
;then, let's split with them.
;if split fails, spit out what was that char it errored on.
$=setoption("casesensitivity","on")
for $ascii=1 to 255
$char=chr($ascii)
$charstring=""+$charstring+$char
if 1<>ubound(split($charstring,$char))
? "Character #" $ascii " (" $char ") failed the split test"
endif
next

get $