Page 1 of 2 12>
Topic Options
#150114 - 2005-10-17 08:51 PM Splitting a String character-by-character
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
Splitting a string such that individual characters are elements of an array.

Is there a simpler way to do this?

Code:
If @LOGONMODE = '1' Break Off Else Break On EndIf
$=SetOption('WrapAtEOL','On')
$=SetOption('NoVarsInStrings','On')
$=SetOption('Explicit','On')

Dim $w,$x,$y,$z

$w='KiXscripting is fun!'

Dim $Array[Len($w)]

$x=Len($w)

For $y=0 to $x
$Array[$y]=SubStr($w,$y,1)
Next

For Each $z in $array
?$z
Next

Sleep 0.75

Quit


Top
#150115 - 2005-10-17 09:04 PM Re: Splitting a String character-by-character
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I always figured that Split() without the second parm should do that but RUud did not agree with me.

You could save a few golf strokes by using $X in the Dim rather than using Len() twice.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#150116 - 2005-10-17 09:18 PM Re: Splitting a String character-by-character
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
68 keystrokes with spaces, not including the value of $x...
Code:
$x='KiXscripting is fun!'
Dim $y[Len($x)]
For $z=0 to Ubound($y)
$y[$z]=SubStr($x,$z,1)
Next

90s to the next fun topic

Top
#150117 - 2005-10-17 11:11 PM Re: Splitting a String character-by-character
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Your solution has an extraneous element in your array. This one is better...

Code:

$x='KiXscripting is fun!'
Dim $y[Len($x)-1]
Do
$y[$z]=SubStr($x,$z+1,1)
$z=$z+1
Until $z=Len($x)


Top
#150118 - 2005-10-17 11:12 PM Re: Splitting a String character-by-character
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Les, I agreed with you...
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#150119 - 2005-10-17 11:23 PM Re: Splitting a String character-by-character
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
...so did Jooel so do we only need one more for a quorum?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#150120 - 2005-10-17 11:24 PM Re: Splitting a String character-by-character
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
I agree as well.
Top
#150121 - 2005-10-18 12:14 AM Re: Splitting a String character-by-character
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
chris' code shortened:
Code:

$x='KiXscripting is fun!'
Dim $y[0^$x]
For $z=1 to 0^$x
$y[$z]=SubStr($x,$z,1)
next

_________________________
!

download KiXnet

Top
#150122 - 2005-10-18 12:16 AM Re: Splitting a String character-by-character
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Lonkenized
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150123 - 2005-10-18 12:17 AM Re: Splitting a String character-by-character
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, sorry.
you are right, that's the word
_________________________
!

download KiXnet

Top
#150124 - 2005-10-18 01:25 AM Re: Splitting a String character-by-character
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Quote:

^ Performs a bitwise mathematical XOR operation on two numbers.




Code:
 Dim $y[0^$x] 



What's going on here? Why does this equate to the length of the $x?

Top
#150125 - 2005-10-18 03:13 AM Re: Splitting a String character-by-character
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Again. That leaves an extra, empty element in the array.
Top
#150126 - 2005-10-18 03:20 AM Re: Splitting a String character-by-character
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Here is corrected code.

Code:

$x='KiXscripting is fun!'
Dim $y[(0^$x)-1]
For $z=0 to (0^$x)-1
$y[$z]=SubStr($x,$z+1,1)
Next


Top
#150127 - 2005-10-18 04:11 AM Re: Splitting a String character-by-character
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Lonkenized:
Code:

$x='KiXscripting is fun!'
Dim $y[0^$x]
For $z=1 to 0^$x
$y[$z]=SubStr($x,$z,1)
Next



Christallized:
Code:

$x='KiXscripting is fun!'
Dim $y[(0^$x)-1]
For $z=0 to (0^$x)-1
$y[$z]=SubStr($x,$z+1,1)
Next



Now it has been 69ed:
Code:

$='KiXscripting is fun!'
Dim $y[0^$]
For $z=0 to (0^$)-1
$y[$z]=SubStr($,$z+1,1)
Next

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#150128 - 2005-10-18 09:56 AM Re: Splitting a String character-by-character
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Now it has been 69ed:




Unfortunately you've also broken it as you've re-introduced the extra blank array element.

You can remove one set of parentheses to reduce the golf score by 2 though:
Code:
$='KiXscripting is fun!'
Dim $y[(0^$)-1]
For $z=1 to 0^$
$y[$z-1]=SubStr($,$z,1)
Next



Using "^" to get the string length is a neat trick. Anyone considering using it in production should check with Ruud that it will be supported going forward as if it is unintentional the feature might disappear.

Top
#150129 - 2005-10-18 04:29 PM Re: Splitting a String character-by-character
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
JEBUS! Ya'll having toooo much fun.

I did 68 characters on my 2nd try but it was wrong. Chris came along and fixed it but it cost him a stroke. Then Howard ended up at 61 strokes with an undocumented function call courtesy of Jooel! Will the madness never end?!?

And how many strokes if Split() reacted to the absence of a 2nd option by splitting the string character-by-character?

Thanks for the lessons, guys.

Top
#150130 - 2005-10-19 01:55 AM Re: Splitting a String character-by-character
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hi Richard,
good try but, i am sorry to say that yours is the only one that is truely broken. Your attempt drops the first character and doubles the last. Chris's and mine produce an array of twenty, yours produces an array of 21.

give it a try:
Code:

;Lonkenized:
?"Start Lonkenized:"
$x='KiXscripting is fun!'
Dim $y[0^$x]
For $z=1 to 0^$x
$y[$z]=SubStr($x,$z,1)
?$y[$z]
Next
?"Finish"
?"Total =:"+$z
Get $a
?
;Christallized:
?"Start Christallized:"
$x='KiXscripting is fun!'
Dim $y[(0^$x)-1]
For $z=0 to (0^$x)-1
$y[$z]=SubStr($x,$z+1,1)
?$y[$z]
Next
?"Finish"
?"Total =:"+$z
Get $a
?
;Now it has been 69ed:
?"Start 69ed:"
$='KiXscripting is fun!'
Dim $y[0^$]
For $z=0 to (0^$)-1
$y[$z]=SubStr($,$z+1,1)
?$y[$z]
Next
?"Finish"
?"Total =:"+$z
Get $a
?
;Now Richard:
?"Start Richard:"
$='KiXscripting is fun!'
Dim $y[(0^$)-1]
For $z=1 to 0^$
$y[$z-1]=SubStr($,$z,1)
?$y[$z]
Next
?"Finish"
?"Total =:"+$z
Get $a

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#150131 - 2005-10-19 06:44 AM Re: Splitting a String character-by-character
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
that's weird as the richie's code really seems to be the correct one and works just as chris' code but indeed saves some extra strokes.

what comes to the trick, len() is not a long keyword and thus using it is not always (if ever) reasonable.

anyways, the trick itself...
someone may try to explain why it works like it does (richie? )
thinking how bits translate from string to binary format for the comparison and then back to decimal does not work.
at least not in my mind.
my mind says the number should be lot higher.
_________________________
!

download KiXnet

Top
#150132 - 2005-10-19 10:00 AM Re: Splitting a String character-by-character
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Hi Richard,
good try but, i am sorry to say that yours is the only one that is truely broken.




It's a better try than you think as it is working correctly

I think that you are making the mistake of assuming that array subscripts start at 1 - they don't, they start at 0.

Here is the code again with the proof that I used to check the array before I posted it:
Code:
$='KiXscripting is fun!'
Dim $y[(0^$)-1]
For $z=1 to 0^$
$y[$z-1]=SubStr($,$z,1)
Next

For Each $ in $y
"'"+$+"'" ?
Next

Exit 0



Try the proof on your code and you will find an additional blank element.

Top
#150133 - 2005-10-19 03:27 PM Re: Splitting a String character-by-character
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hi Richard,
I apologize, when I was comparing code I missed ‘$y[$z-1]=SubStr($,$z,1)’, specifically the -1. Also I am intrigued to see the way you print the result, I did not know you could put the question mark at the end of the statement, why does that work?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 293 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.072 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org