Series manipulation Help
[1/7] from: charles::wardell::dendrite::com at: 9-Nov-2004 12:29
Series manipulation.
Lst: ["Charlie" 120912 "Peter" 239823 "Sam" 9238923 "Tiana" 92348]
I want to find "Sam" and retrieve his ID
I them want to remove Sam his ID from Lst: and append it to the end of
Lst:
I was working down this path, but I am sure there must be an easier way:
i: index? find Lst key
name: pick Lst i
ID: pick Lst i + 1
top: copy/part i - 1
bottom: find Lst Key
remove bottom
remove bottom
newLst: join top bottom
[2/7] from: SunandaDH:aol at: 9-Nov-2004 12:52
Charles:
> I was working down this path, but I am sure there must be an easier way:
here's one possibility:
lst: ["Charlie" 120912 "Peter" 239823 "Sam" 9238923 "Tiana" 92348]
target: "sam"
target-id: select lst target
if not none? target-id [
alter lst target
alter lst target-id
append lst target
append lst target-id
]
probe lst
This works provided there are no duplicate ids in lst. It would fail on a lst
like this where Charlie and Sam have the same id:
lst: ["Charlie" 11111 "Peter" 239823 "Sam" 11111 "Tiana" 92348]
Sunanda
[3/7] from: charles:wardell:dendrite at: 9-Nov-2004 13:31
Thank you for such a quick reply and I like your clever approach. but I
think I will run into problems if the target-id is non-unique.
Any suggestions?
[4/7] from: greggirwin::mindspring::com at: 9-Nov-2004 11:38
Re: [series//move] Series manipulation Help
Hi Charles,
WC> Lst: ["Charlie" 120912 "Peter" 239823 "Sam" 9238923 "Tiana" 92348]
WC> I want to find "Sam" and retrieve his ID
WC> I them want to remove Sam his ID from Lst: and append it to the end of
WC> Lst:
I started tinkering with something a while back that may be useful to
you. Consider it a starting point if nothing else.
move: func [
"Moves the first instance of value, if found, to a new position in the series."
series [series!]
value
/head "Move to the head of the series"
/tail "Move to the tail of the series"
/to "Move to an absolute position in the series"
index [number! logic! pair!] "Can be positive, negative, or zero"
/skip "Move forward or backward from the current position"
offset [number! logic! pair!] "Can be positive, negative, or zero"
/part "Move the given number of items"
range [number! series! pair!]
;/all "move all instances of value" ; ???
/local pos dest sw*
] [
sw*: system/words
either none? pos: find/only series value [none] [
either part [
value: copy/part pos range
remove/part pos range
][
value: first pos
remove pos
]
dest: any [
all [head sw*/head series]
all [tail sw*/tail series]
all [to at series index]
all [skip sw*/skip pos offset]
]
either part [insert dest :value] [insert/only dest :value]
]
]
Anyone else who wants to improve it, please do so; or let me know what
doesn't work--or what you don't like--and I'll make notes here.
-- Gregg
[5/7] from: SunandaDH:aol at: 9-Nov-2004 14:33
Re: Series manipulation Help
Charles:
> but I think I will run into problems if the target-id is non-unique.
Then I'd apply the following hack:
lst: ["Charlie" 11111 "Peter" 239823 "Sam" 11111 "Tiana" 92348]
target: "Sam"
target-id: select lst target
if not none? target-id [
remove at lst 1 + index? find lst target
alter lst target
append lst target
append lst target-id
]
This code is "inefficient" in that in searches lst three times (select + find
+ alter). You could attempt to "optimise" it by saving the initial position
in a variable. But unless you are intending to execute it a zillion times a day
on megabyte long blocks, it's hardly worth the effort.
Should work if you have duplicate target-ids and/or duplicate targets (more
than one "sam").
Likely to go astray if a target can be the same as a target-id (not currently
a problem as they are in different domains -- string! vs number!)
Sunanda.
[6/7] from: rotenca:telvia:it at: 9-Nov-2004 20:41
You could try this function:
cut: func [series /part range][
range: any [range 1]
part: copy/part series range
remove/part series range
part
]
b: [1 2 3 4 5 6]
if tmp: find b 3 [
x: cut/part tmp 2
insert tail tmp x
]
---
Ciao
Romano
[7/7] from: charles::wardell::dendrite::com at: 9-Nov-2004 15:54
That will work great! Thank you Romano, Gregg, and Sunanda..
I really appreciate your help.