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: tim:johnsons-web at: 21-Oct-2003 8:05

Hello REBOLS: Thanks for all of the input. Andrew and I got into an OTL thread, which I'll soon route back to the list. FYI: I'm toying with an idea. I have already developed an object approach with uses data structures to generate 'ML code (Andrew's 'ML dialect). It's still in beta (gamma really), but promises to cut down on coding and I've developed a parallel resource in python. Think Item #4 in the Designers Tip Sheet: "Data Drives It" The next step is to create the data structures from a text file, ultimately to perhaps be produced by non-programmers, thus a bullet/idiot-proof approach must be taken. Will post further when I hear back from Andrew. tim * Carl Read <[carl--cybercraft--co--nz]> [031021 00:35]:
> On 21-Oct-03, Tom Conlin wrote: > > > On Mon, 20 Oct 2003, 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)} > >> /default _dval[any-type!] {custom default value (NONE is > >> default)} > >> ][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 > > > could try > > > append al-block load value > > Hmm - I should've looked at the function properly. :) (See my > previous post, which you can disregard due to what I say below about > syntax errors using load.) > > With "... load value" as Tom suggests above, names such as "Tim > Johnson" would be converted to two words and not a string, so a check > for spaces and perhaps other characters would be needed to prevent > that. ie, you'd return a string if a space was discovered. Another > problem would be syntax errors when load hits text it can't recognise > as a valid datatype. So, an error check would be needed too, with a > string returned if you did get an error. > > I get the feeling though you'd be better to force what datatype they > should be based on the names encounted, (ie, age = decimal, > occupation = string), as the format of your data may not ever be > quite right to always do a correct conversion. > > -- > Carl Read > > -- > To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com