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

Building an Associate List from a string

 [1/10] from: tim::johnsons-web::com at: 20-Oct-2003 17:12


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 -- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com

 [2/10] from: andrew:martin:colenso:school at: 24-Dec-2003 22:42


Tim wrote:
> 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?
Rebol [] test: "age^-54^-name^-Tim Johnson^-occuptation^-coder^-Salary^-USD$100000.00" probe parse/all test [ some [ copy Word to tab skip (probe Word: to word! Word) [ copy Value integer^ (Value: to integer! Value) | copy Value money^ (Value: to money! Value) | copy Value [to tab | to end] ] (probe Value) [tab | end] ] end ] halt You'll need my %Patterns.r script to get values for integer^ and money^ (there's other patterns there as well). You need to put the above into a function and make 'Word and 'Value into local words and take out 'probe-s. I hope that helps! Andrew J Martin Attendance Officer Speaking in tongues and performing miracles Colenso High School Arnold Street, Napier. Tel: 64-6-8310180 ext 826 Fax: 64-6-8336759 http://colenso.net/scripts/Wiki.r?AJM http://www.colenso.school.nz/ DISCLAIMER: Colenso High School and its Board of Trustees is not responsible (or legally liable) for materials distributed to or acquired from user e-mail accounts. You can report any misuse of an e-mail account to our ICT Manager and the complaint will be investigated. (Misuse can come in many forms, but can be viewed as any material sent/received that indicate or suggest pornography, unethical or illegal solicitation, racism, sexism, inappropriate language and/or other issues described in our Acceptable Use Policy.) All outgoing messages are certified virus-free by McAfee GroupShield Exchange 5.10.285.0 Phone: +64 6 843 5095 or Fax: +64 6 833 6759 or E-mail: [postmaster--colenso--school--nz]

 [3/10] from: andrew:martin:colenso:school at: 24-Dec-2003 22:42


Earlier I wrote:
> You need to put the above into a function and make 'Word and 'Value
into local words and take out 'probe-s. Something like this: Rebol [] Test: "age^-54^-name^-Tim Johnson^-occupation^-coder^-Salary^-USD$100000.00" Parse-List: function [String [string!] /With Delimiter [string! char!]] [Block Word Value] [ Delimiter: any [Delimiter tab] Block: copy [] all [ parse/all String [ some [ copy Word to Delimiter skip (Word: to word! Word) [ copy Value integer^ (Value: to integer! Value) | copy Value money^ (Value: to money! Value) | copy Value [to Delimiter | to end] ] ( insert tail Block reduce [Word Value] ) [Delimiter | end] ] end ] Block ] ] probe parse-list Test halt Which returns: [age 54 name "Tim Johnson" occupation "coder" Salary USD$100000.00] Andrew J Martin Bored Attendance Officer Speaking in tongues and performing miracles Colenso High School Arnold Street, Napier. Tel: 64-6-8310180 ext 826 Fax: 64-6-8336759 http://colenso.net/scripts/Wiki.r?AJM http://www.colenso.school.nz/ DISCLAIMER: Colenso High School and its Board of Trustees is not responsible (or legally liable) for materials distributed to or acquired from user e-mail accounts. You can report any misuse of an e-mail account to our ICT Manager and the complaint will be investigated. (Misuse can come in many forms, but can be viewed as any material sent/received that indicate or suggest pornography, unethical or illegal solicitation, racism, sexism, inappropriate language and/or other issues described in our Acceptable Use Policy.) All outgoing messages are certified virus-free by McAfee GroupShield Exchange 5.10.285.0 Phone: +64 6 843 5095 or Fax: +64 6 833 6759 or E-mail: [postmaster--colenso--school--nz]

 [4/10] from: tim:johnsons-web at: 20-Oct-2003 18:20


* Andrew Martin <[andrew--martin--colenso--school--nz]> [031020 18:12]: Thanks Andrew! :-) Special thanks for the raise... tim
> test: "age^-54^-name^-Tim > Johnson^-occuptation^-coder^-Salary^-USD$100000.00"
<<quoted lines omitted: 36>>
> 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

 [5/10] from: antonr:iinet:au at: 21-Oct-2003 13:29


Tim, you could use FORM instead of TO-STRING. It's shorter and native as well. Actually, now I look you can optimize this a bit: sep: form either with [delimiter][tab] Anton.

 [6/10] from: tomc:darkwing:uoregon at: 20-Oct-2003 23:24


On Mon, 20 Oct 2003, Tim Johnson wrote:
> Hello REBOLS: > I need some advice :-)
<<quoted lines omitted: 36>>
> To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
could try append al-block load value

 [7/10] from: carl:cybercraft at: 24-Dec-2003 22:42


On 21-Oct-03, Tim Johnson wrote:
> Hello REBOLS: > I need some advice :-)
<<quoted lines omitted: 5>>
> 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]
<<quoted lines omitted: 21>>
> 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

 [8/10] from: carl:cybercraft at: 24-Dec-2003 22:42


On 21-Oct-03, Tom Conlin wrote:
> On Mon, 20 Oct 2003, Tim Johnson wrote: >> Hello REBOLS:
<<quoted lines omitted: 36>>
> 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

 [9/10] 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:
<<quoted lines omitted: 56>>
> 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

 [10/10] from: maximo:meteorstudios at: 21-Oct-2003 12:31


> -----Original Message----- > From: Tim Johnson [mailto:[tim--johnsons-web--com]]
<<quoted lines omitted: 4>>
> Think Item #4 in the Designers Tip Sheet: > "Data Drives It"
that's the idea in liquid. Data changes cause the reactions in your application. gadgets in liquid-vid just become data handlers which pump user's actions into data changes. The rest of the application then reacts on its own, since you've instructed what to do with the data.. you know, when I come to think of it, that's somewhat related to AI neural network thinking. when setup correctly, you can even have some liquids filling up other liquids in cascade. Some care must be taken for infinite cycles, but liquid has refinements to control that. with this perspective in mind, liquid-vid becomes the sensory engine and liquid is the neural network. spills then do the thinking and can relate to a context, so that decisions can be based on experience... Can anyone with real AI experience validate the above claims? -MAx

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted