Re: sequence and strings (newbie help wanted)
- Posted by jc at cknet.net Mar 15, 2001
- 447 views
Thank you very much Derek, a wonderful reply. I will try to digest this. Jim Chapman ----- Original Message ----- From: "Derek Parnell" <ddparnell at bigpond.com> To: "EUforum" <EUforum at topica.com> Sent: Wednesday, March 14, 2001 10:25 PM Subject: RE: sequence and strings (newbie help wanted) Hi there Jim, here is an alternative program... ------------------ include get.e sequence cmd integer vwait cmd =mmand_line() for i =to length(cmd) do puts(1, cmd[i] & " ") end for vwait =it_key() ------------------- The difference are ... -- define cmd as a sequence because this is what command_line() returns. -- define vwait as an integer because this is what wait_key() returns. -- The command_line() function returns a single sequence, and that sequence contains a list of sequences - one for each word on the command line plus a couple of special words. -- I use a for/end for construct to type out each subsequence contained in cmd -- I don't have to define the 'i' because the for automatically does this. -- The length() function returns the number of sequences inside cmd -- I use cmd & " " to append a single blank after each typed word. > I get an error message saying that I have a sequence in a > character string. This is because the puts() function expects a special sort of sequence, one that does NOT have subsequences. The command_line() function returns a sequence that HAS subsequences, so we have to extract them to use in puts(). That's what the for loop does. cmd[1] references the first element in cmd cmd[2] references the second element in cmd etc ... > In my ex.err I find: > > cmd =7'C',58':',92'\',69'E',85'U',80'P',72'H',79'O',82'R',73'I', > 65'A',92'\',66'B',73'I',78'N',92'\',101'e',120'x',119'w',46'.',101'e', > 120'x',101'e'},{67'C',58':',92'\',69'E',85'U',80'P',72'H',79'O',82'R', > 73'I',65'A',92'\',66'B',73'I',78'N',92'\',102'f',105'i',114'r',115's', > 116't',46'.',101'e',120'x',119'w'}} This show the sequence (and subsequences) as lists of numbers enclosed in {} brackets. A string is a special sequence type in Euphoria. It is simply a sequence that has no subsequences (and usually each element is an integer in the range 0 - 255). Even though its special, there is no special handling by Euphoria - its only special to the functions which expect it. Now some general stuff. A sequence is a list of zero or more objects. An object is either a sequence or an atom. A atom is a number. There is a special type of atom called an integer that can only contain a 31-bit whole number. Atoms can hold floating point type numbers as well. These are the only data types native to Euphoria. Anything else (such as a string) is not native and must be handled in your (or someones) Euphoria code. Euphoria provides a little bit of help by allowing you to define your own data types but only allows one method for them, basically a 'is a type of' method that returns either true or false. eg. ---------------- type string(object x) if atom(x) then -- x cannot be an atom return 0 end if for i = to length(x) do -- every element must be an integer if not integer(x[i]) then return 0 end if -- and have a value from 0 to 255 if x[1] < 0 or x[i] > 255 then return 0 end if end for -- if I get to here then 'x' is a true string. return 1 end type string test1, test2 -- This should work test1 =abc" -- As should this test1 ='a','b','c'} -- And this test1 =1,2,3} -- This should fail with a "type_check failure" test2 =1,test1,3} -- contains a subsequence. -- This should fail with a "type_check failure" test2 =1, 2.5, 3} -- contains a non-integer -- This should fail with a "type_check failure" test2 =1, -2, 3} -- contains a negative integer -- This should fail with a "type_check failure" test2 =1, 2, 300} -- contains a large integer ------------------------ > Also, looking for a word() function, but not finding one in Euphoria. Is > there a built in function that will parse through command_line() returning > the Nth word ? Not really required as command_line() already does this. The first two elements returned are the interpreter's name that is running and the name of the euphoria program running. ----------- cheers Derek