Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Building an Associate List from a string

From: carl:cybercraft at: 24-Dec-2003 22:42

On 21-Oct-03, Tim Johnson wrote:
> Hello REBOLS: > I need some advice :-) > I've written a function that converts a delimited string into an > associative list. > Code and example console session is below: ;; > > make-al: function[ {Builds an associative list from a delimited > string} > str[string!] {delimited string} > /with delimiter[string! char!] {custom delimiter (TAB is
> /default _dval[any-type!] {custom default value (NONE is
> ][tmp al-block sep dval][ > sep: either with[to-string delimiter][to-string TAB] > dval: either default[_dval][none] > al-block: copy [] > tmp: parse/all str sep > if not even? length? tmp[append tmp dval] > foreach [name value] tmp[ > append al-block to-word name > append al-block value > ] > al-block > ] > test: "age^-54^-name^-Tim Johnson^-occuptation^-coder" > probe make-al test > ;; = Console session > == [age "54" name "Tim Johnson" occuptation "coder"] > ;; Now, so far it is a piece of cake, but this is where > ;; I falter: > I would like to employ a strategy that converts any value > to the best guess for a rebol datatype. IOWS, "54"[string!] > would become 54[integer!] > It occurs to me that a brute-force method would be to process > via a nested attempt[any[]] loop. > Any more "elegant" ideas? > thanks > tim
Hi Tim, If you could first enclose the text you want to end up as strings in speechmarks, (maybe using parse), a simple load may be all you'd need. ie...
>> test: {age^-54^-name^-"Tim Johnson"^-occuptation^-"coder"}
== {age^-54^-name^-"Tim Johnson"^-occuptation^-"coder"}
>> load test
== [age 54 name "Tim Johnson" occuptation "coder" ] It all depends on how well-ordered your data is, I guess. -- Carl Read