Poke and Pick and binary! Part 2
[1/2] from: reboler::programmer::net at: 4-May-2001 11:45
Thanx! Anton, Joel Neely, and "Larry Palmiter". I now understand a little
more.
But...
The reason I want to do this is to swap elements of _any_ series!
The previous code was part of the following function, the purpose of which
is to swap elements in any type of series!
First, if I add the "to-char" refinement then other types of series no
longer work (try adding the "to-char" change and then swap elements of [1 2
3 4 5 6 7 8 9 0] !).
Second, is there a rebol native or mezzanine that does this already?
Third, if the answer to "2" is "false", any suggestions on changing the
s-swap function so that it will swap elements of any type of series (the
primary focus of this is to be able to swap block!s string!s and compressed
string!s - which are binary!)
;Here is the function as it stands**********
s-swap: func [{swaps the elements of two series at the given index
positions}
a [series!] "1st series"
index-a [integer!] "index position to swap in 1st series"
b [series!] "2nd series"
index-b [integer!] "index position to swap in 2nd series"
/local holder
] [
holder: pick a index-a
poke a index-a pick b index-b
poke b index-b holder
]
;****************
[2/2] from: joel:neely:fedex at: 4-May-2001 11:38
Hi, Alan,
...try this.
alan parman wrote:
> Thanx! Anton, Joel Neely, and "Larry Palmiter". I now understand a little
> more.
>
> But...
>
> The reason I want to do this is to swap elements of _any_ series!
> The previous code was part of the following function, the purpose of which
> is to swap elements in any type of series!
>
swapper: func [s [series!] i [integer!] j [integer!] /local si sj] [
si: copy/part at s i 1
sj: copy/part at s j 1
change at s i sj
change at s j si
]
which behaves as follows...
>> foo: "this is a Test"
== "this is a Test"
>> swapper foo 1 11 foo
== "This is a test"
>> foo: to-binary "this is a Test"
== #{7468697320697320612054657374}
>> swapper foo 1 11 foo
== #{5468697320697320612074657374}
>> to-string foo
== "This is a test"
Improvements, anyone?
-jn-