1. sequence and strings (newbie help wanted)
- Posted by jc at cknet.net Mar 14, 2001
- 448 views
- Last edited Mar 15, 2001
Hello, Just begining with Euphoria, this is my first script. Trying to get the command_line() and parse through it. Thought I would start by throwing the contents of command_line() on the screen: include get.e object cmd cmd = command_line() puts(1, cmd) vwait = wait_key() -- putting the wait_key() in just so my screen remain up I get an error message saying that I have a sequence in a character string. In my ex.err I find: cmd = {{67'C',58':',92'\',69'E',85'U',80'P',72'H',79'O',82'R',73'I', 116't',46'.',101'e',120'x',119'w'}} Is this how Euphoria internally stores a string/sequence. Obviously I don't understand something fundamental here with strings and sequences. 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 ? TIA Jim Chapman
2. 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
3. Re: sequence and strings (newbie help wanted)
- Posted by Kat <gertie at PELL.NET> Mar 15, 2001
- 460 views
On 14 Mar 2001, at 21:40, jc at cknet.net wrote: > > > Hello, > Just begining with Euphoria, this is my first script. Trying to get the > command_line() and parse through it. Thought I would start by throwing the > contents of command_line() on the screen: > > include get.e > object cmd > cmd Page break? ommand_line() > puts(1, cmd) > > vwait > -- putting the wait_key() in just so my screen remain up > > I get an error message saying that I have a sequence in a character string. > In my ex.err I find: > > cmd > 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'}} That's how Eu stored the command line, alright. The debug printout shows the bytes, followed by printable chars. Maybe what you wanted to use was gets() and puts()? > Is this how Euphoria internally stores a string/sequence. Obviously I don't > understand something fundamental here with strings and sequences. Strictly speaking, there are no strings in Eu, but we can use sequences as strings. A sequence is a sequence of bytes, for any purpose. They can even be nested inside each other. Like this: seq = {this is {sequence #2} sequence #1} so: seq[1] = 't' seq[9] = "sequence #2" seq[9][2] = 'e' There are a number of string handling librarys in the Eu archives to make sequences more string-like. There are prolly 4 or 5 of them, from basics, to very flexable strings, to calling words tokens, to regexp stuff that is beyond me. Just hold onto your hat when you get to print(), printf(), sprint(), sprintf(), print.e, printseq(), displayseq(), etc, etc... > 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 ? Problem with sequences (for newcomers) is their flexability. While you can put words into a sequence, you'd need a string lib to get them back out, since the string lib knows about whitespace, periods, commas, etc.. Unless you stored a string as: str= {"word1","word2","word3"} in which case, str[2] = "word2" the command_line is: cmd={"path","path","the first optional parm","another optional parm"} so cmd[1] = the full path of the exw.exe cmd[2] = the path and name of your program cmd[3..length(cmd)] = the various parms you want your program to have. The closest thing to word() in strings that i know of is the lib i submitted at: http://www.rapideuphoria.com/strtok.zip since i call words in natural language "tokens" of the language, items of meaning, if you know what i mean. You'll find a lot of token functions in there. /me thanks everyone who contributed code to it again. Look at: for more. I don't see strings.e by Normand Blais there tho, and it isn't found in the rds archive search either,,, so get string0_9.e from inside http://www.rapideuphoria.com/mirc.zip and you'll be all set. The functions removed to make string0_9.e from string.e is in strtok.zip, in different code though. Hope this helps, Kat