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

[REBOL] Re: Beginner's automatic type conversion

From: joel:neely:fedex at: 23-Sep-2002 6:29

Hi, Laurent, Whenever possible, I suggest letting REBOL do the work for you... See below. Laurent Giroud wrote:
> What I am doing is very simple, I get string data and store it > this way : > > trans: [ > ["09:33" "12.30" "1 270"] ; [string! string! string!] > ["09:34" "12.31" "4 450"] > ... > ] > > and need to convert it (for faster manipulation later on) into : > [ > [9:33 12.30 1270] ; [time! decimal! integer!] > [9:34 12.31 4450] > ... > ] >
The only tricky bit is the embedded space in the third element of each triplet, but you already know how to deal with that!
>> trans: [
[ ["09:33" "12.30" "1 270"] ; [string! string! string!] [ ["09:34" "12.31" "4 450"]] == [ ["09:33" "12.30" "1 270"] ["09:34" "12.31" "4 450"]] Using the first two triplets of your sample data...
>> foreach row trans [
[ foreach item row [ [ foo: load trim/all copy item [ print [item foo type? foo] [ ] [ ] 09:33 9:33 time 12.30 12.3 decimal 1 270 1270 integer 09:34 9:34 time 12.31 12.31 decimal 4 450 4450 integer Unless the input uses some strange representation (e.g., embedded spaces which must be removed) the REBOL lexical scanner will recognize the data for you. -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]