#150114 - 2005-10-17 08:51 PM
Splitting a String character-by-character
|
xpanmanx
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
|
|
|
|
#150116 - 2005-10-17 09:18 PM
Re: Splitting a String character-by-character
|
xpanmanx
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.
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
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Les, I agreed with you...
|
|
Top
|
|
|
|
#150122 - 2005-10-18 12:16 AM
Re: Splitting a String character-by-character
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#150124 - 2005-10-18 01:25 AM
Re: Splitting a String character-by-character
|
Allen
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
|
|
|
|
#150126 - 2005-10-18 03:20 AM
Re: Splitting a String character-by-character
|
Chris S.
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
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
|
|
Top
|
|
|
|
#150128 - 2005-10-18 09:56 AM
Re: Splitting a String character-by-character
|
Richard H.
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
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
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
|
|
Top
|
|
|
|
#150132 - 2005-10-19 10:00 AM
Re: Splitting a String character-by-character
|
Richard H.
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
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?
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 293 anonymous users online.
|
|
|