1. sequences and string, thought I understood!
- Posted by jc at cknet.net Mar 16, 2001
- 456 views
Hello, I read and thought I understood your replies , but this doesn't work: ------------------------------------- include get.e with trace trace(1) integer vwait sequence cmd, varray cmd = command_line() varray = {} for i =1 to length(cmd) do varray[i] = cmd[i] end for for i = 1 to length(varray) do puts(1, varray[i] &"\n" ) end for vwait = wait_key() Command_line()' returns a sequence of sequences which I can parse through with cmd[i] giving me one word at a time. However when I create what I see as the exact same structure, using a For loop, it appears EU sees this as a sequence of single value ascii codes. If I do this v = {"this", "us", "a", "test"} EU sees this as a sequence of sequences, but again, if I construct this same constuct with a For loop as in: sequence v1, v2 object vwait v1 = {"this", "is", "a", "test"} v2 = {} for i = 1 to length(v1) do if i = length(v1) then v2 = v2 & "\"" & v1[i] & "\"" else v2 = v2 & "\"" & v1[i] &"\"," end if end for for i = 1 to length(v1) do puts(1, v1[i] & "\n") end for for i = 1 to length(v2) do puts(1, v2[i] & "\n") end for vwait = wait_key() EU sees v1 as a sequence of sequences, but v2 as a plain jane sequence. Don't understand............ TIA Jim Chapman
2. Re: sequences and string, thought I understood!
- Posted by Kat <gertie at PELL.NET> Mar 16, 2001
- 448 views
On 16 Mar 2001, at 0:32, jc at cknet.net wrote: This is about all i could get from your email, it was filled with non-printable ascii codes that Pegasus interpreted as page breaks and other stuff..... > Trying to assign varray a sequence of sequences, EU apparently sees this as > nothing more than a plain jane sequence of ascii codes. 'cmd Command_line()' > returns a > sequence of sequences which I can parse through with cmd[i] giving me one word > at a time. > However when I create what I see as the exact same structure, using a For > loop, it appears > EU sees this as a sequence of single value ascii codes. A sequence is just that, a sequence of bytes, ascii or otherwise. How you see them depends on how you print them. Did you look up the various methods of printing i mentioned in the earlier email? Kat
3. Re: sequences and string, thought I understood!
- Posted by Derek Parnell <ddparnell at bigpond.com> Mar 16, 2001
- 448 views
Hi Jim, I'm not sure if its plain spelling errors or the email has dropped some characters so I'm making some guesses at what your example code is supposed to be. >------------------------------------- >include get.e > with trace >trace(1) > >integer vwait >sequence cmd, varray >cmd =command_line() >varray ={} >for i = 1 to length(cmd) do > varray[i] =cmd[i] >end for > >for i = to length(varray) do > puts(1, varray[i] &"\n" ) >end for > >vwait =wait_key() >What I'm trying to do is simply parse cmd into it's various word >components and places these in variables. Of course as I don't >know how many command line parameters there will be, I can't >pre-define the variables to hold these values. Firstly, you don't have to do this. Because that's what command_line() function already does. .. cmd = command_line() The line above creates a sequence (scalar array, if you like) such that each of its elements is a word from the program's command line. Just what you are trying to do. But aside from that, let's do this as an acedemdic exercise. Your program is almost correct. Just replace ... varray ={} with varray = repeat(0, length(cmd)) and it will work. Why? Because in your code you initialise varray with a sequence of zero elements. "varray = {}" simply assigns an empty sequence to varray. However, later on in the code, you try to assign elements beyond the zeroth one "varray[i] = cmd[i]" but they don't exist. So the program fails. Euphoria has two simple ways to get around this. The first, if you know how many elements will be required, and in this case we do, is to initialise the sequence to one that has the desired number of elements. That's what the repeat() function does. It takes two parameters, the first is an object (either a sequence or an atom), and the second is the number of repetitions of that object you want. The repeat() function always returns a sequence containing 'x' repetitions of the object you specified. In this case, we know that we want, the same number of elements that is in "cmd" sequence, so we can use "length(cmd)" for this. The object we use is irrelevent because we are going to replace it in the assignment anyhow. Thus I came up with "varray = repeat(0, length(cmd))". The second offerring from Euphoria is the ability to concatenate elements on to a sequence. This is often used when we don't know how many elements we are going to end up with. Its a bit slower but very, very useful. We have three different ways to concatenate. a] The append() function. This adds to the right end of a sequence, a single element. Example: "varray = append(varray, cmd[i])" In this example, cmd[i] is added to the end of varray. b] The prepend() function. This adds to the left end of a sequence, a single element. Example: "varray = prepend(varray, cmd[i])" In this example, cmd[i] is added to the front of varray. c] The & operation. This adds to the right end of a sequence, all the elements in another sequence. Example: "varray = varray & cmd[i]" In this example, all the elements in cmd[i] are added to the end of varray. To see the difference between these operations, let's imagine that cmd has the value {"derek","parnell"} and varray is initialised to {}. cmd = {"derek", "parnell"} varray = {} varray = append(varray, cmd[1]) varray = append(varray, cmd[2]) leaves varray with the value {"derek","parnell"} cmd = {"derek", "parnell"} varray = {} varray = prepend(varray, cmd[1]) varray = prepend(varray, cmd[2]) leaves varray with the value {"parnell","derek"} cmd = {"derek", "parnell"} varray = {} varray = varray & cmd[1] varray = varray & cmd[2] leaves varray with the value "derekparnell" This last one is because cmd[1] is a sequence of 5 numbers that make up the string "derek", and cmd[2] is a sequence of 7 numbers and when concatenated form a single sequence of 12 numbers that make up the string "derekparnell". Now of course, the simplest way of doing what you wanted is just to assign cmd to varray. ---------------- include get.e with trace trace(1) integer vwait sequence cmd, varray cmd =command_line() varray = cmd for i = to length(varray) do puts(1, varray[i] &"\n" ) end for vwait =wait_key() ----------------------- >Don't understand............ Hope I've help and not confused things even more. It took quite a while for me to understand when to use append() and when to use '&', and I still get them mixed up sometimes. One final example. varray = varray & {cmd[1]} is the same as varray = append(varray, cmd[1]) because {cmd[1]} is a sequence with ONE element - the sequence cmd[1] . ------ Derek Parnell Melbourne, Australia "To finish a job quickly, go slower."
4. Re: sequences and string, thought I understood!
- Posted by jc at cknet.net Mar 16, 2001
- 449 views
- Last edited Mar 17, 2001
Thanks all for your help, I really am beginning to understand........... To Derek Parnell, Your explaination elucidates my question. You mention the three ways to concatenate in EU; Append() Prepend() The & operation In the testing script below: vwait = wait_key() Don't quite understand what is going on here. This was what was throwing me last night, only before your responses I couldn't be EU to return a sequence of sequences, I was just getting the sequence of atoms. Thanks again TIA Jim PS the messages I'm receiving seem to drop the character after an "=". Derek mentioned that my messages where slightly garbled also. I put in an extra blank around the = sign. Maybe that will help.
5. Re: sequences and string, thought I understood!
- Posted by David Cuny <dcuny at LANSET.COM> Mar 16, 2001
- 445 views
- Last edited Mar 17, 2001
Jim wrote: > You mention the three ways to > concatenate in EU; As a rule: 1. Use '&' to concatentate 'strings' 2. Use append to add items to the end of sequences and you will stay fairly safe. Even now, I get into trouble writing: s &= a when I mean: s &= {a} I'd never run into that problem if I had been less clever and just written: s = append( s, a ) -- David Cuny