1. Newbie question. Difficulty understanding a few things.
- Posted by Rob H. <robert.hrosc at gm?il?com> May 25, 2008
- 714 views
Hello. I've been reading "A beginner's guide to euphoria". This question pertains to demo program 43 in that tutorial... I don't quite understand a few things. I would appreciate if somebody could tell me what's going on in this code...
for element = 1 to length(seek_positions) do status = seek(file_id,seek_positions[element][1]-1) if status = 0 then puts(file_id,seek_positions[element][2]) end if
In the second line. I don't understand what the [1]-1 is doing... can somebody please explain? Also, what's the [2] in the 4th line doing...? Then Further down in the code...
while compare(input_line,-1) != 0 do puts(1,input_line) input_line = gets(file_id) end while
Why are they comparing input_line to -1?? I'm sorry. I'm a newbie, and there isn't enough documentation available about Euphoria to satisfy all of my questions... Thanks for the help!
2. Re: Newbie question. Difficulty understanding a few things.
- Posted by Mike777 <anon4321 at gmail?c?m> May 25, 2008
- 686 views
Rob H. wrote: > > > Hello. I've been reading "A beginner's guide to euphoria". > This question pertains to demo program 43 in that tutorial... I don't quite > understand a few things. I would appreciate if somebody could tell me what's > going on in this code... > > }}} <eucode> > for element = 1 to length(seek_positions) do > status = seek(file_id,seek_positions[element][1]-1) > if status = 0 then > puts(file_id,seek_positions[element][2]) > end if > </eucode> {{{ > In the second line. I don't understand what the [1]-1 is doing... can somebody > please explain? Both this question and the one below require you to understand how sequences work. Think of them as what other languages typically describe as an "array". But it is more like "an array on steroids" as the language interfaces with sequences in ways that other languages do not interface with arrays. And the fact that the language interfaces with sequences provides a lot of power. In the specific line of code above, you are calling a function (seek) which takes two arguments. The second argument is a number. That number is determined by going to the seek_positions sequence and then looking at what would be referenced in another language as an array, with something like "seek_positions(element,1)" and once that number is retrieved, subtract one from that. > Also, what's the [2] in the 4th line doing...? Same as above. > > Then > > Further down in the code... > > }}} <eucode> > while compare(input_line,-1) != 0 do > puts(1,input_line) > input_line = gets(file_id) > end while > </eucode> {{{ > > Why are they comparing input_line to -1?? Do you have the documentation? If you look up the "compare" library routine you will see that it is comparing the value if "input_line" to "-1". > I'm sorry. I'm a newbie, and there isn't enough documentation available about > Euphoria to satisfy all of my questions... Oh, it is there. You just have to get used to using it. Use this in a google search and select the first link: "sequences are it" euphoria That should get you going. Welcome and good luck. Mike > > > Thanks for the help!
3. Re: Newbie question. Difficulty understanding a few things.
- Posted by Mike777 <anon4321 at g?ail.?om> May 25, 2008
- 696 views
Rob H. wrote: > Further down in the code... > > }}} <eucode> > while compare(input_line,-1) != 0 do > puts(1,input_line) > input_line = gets(file_id) > end while > </eucode> {{{ > > Why are they comparing input_line to -1?? I'm not sure my response answered the specific question you were asking, which is "why". That code it traversing through the information/data in the file which is identified by the "file_id". It starts by assuming the value of input_line is something other than -1. If that is a valid assumption, then it executes the next two lines. The first of those next two lines outputs what is in input_line to the console (the console's identifier is "1"). The second line then gets another bit of information from the "file_id" file. Or, at least it tries. If it fails, it will put -1 (as a number) into input_line. Finally, it will then re-execute the while line and do everything over again if input_line is something other than -1. So the answer to your question is that the gets function will fail when the end of file is reached and when it fails it puts -1 into the input_line variable and therefore that line is testing input_line to see when its value is -1 and when it is it stops executing this loop. Mike
4. Re: Newbie question. Difficulty understanding a few things.
- Posted by Rob H. <robert.hrosc at gm?il?com> May 25, 2008
- 710 views
I understand what seek is doing. However. The seek_positions[element] determines where seek is currently "looking" because it's incrementing with the for element loop. So why is the [1] necessary?? Not sure if I'm explaining myself correctly... In the Reference Manual, seek only takes one argument... I'm wondering what the [1] and -1 specifically do.
5. Re: Newbie question. Difficulty understanding a few things.
- Posted by Dan Moyer <danielmoyer at prodi?y?net> May 25, 2008
- 731 views
Rob H. wrote: > > > I understand what seek is doing. However. The seek_positions[element] > determines > where seek is currently "looking" because it's incrementing with the for > element > loop. > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > In the Reference Manual, seek only takes one argument... I'm wondering what > the [1] and -1 specifically do. Rob, the variable "seek_positions" is in effect an ARRAY, as it was created earlier in the demo to contain TWO items per "element", namely, the POSITION of a found vowel in the demo paragraph, and the actual VOWEL itself, so that the vowels, which had been REMOVED from the demo paragraph, could later be RE-INSERTED. So, the counter "element" is looking at each instance of a found vowel, where the FIRST item of each instance is the POSITION, and the SECOND item of each instance is the VOWEL. So, as the counter "element" is incremented, the code first finds WHERE in the demo the vowel has been removed from, namely seek_positions[element][1], and then uses that info to re-insert the actual vowel, which is stored at the SECOND place in each instance of discovered vowel, namely seek_positions[element][2]. Do you understand now why the [1] is being used? The actual sequence "seek_positions" would look like this for the first 5 "elements": {{3,101},{6,101},{7,97},{8,117},{12,111}} Notice the GROUPS of numbers; each group one of the "elements" counter; the FIRST number in each of those groups is the POSITION of a found vowel, and the SECOND number is the ascii representation of the vowel that was found at that postition. So the "elements" counter finds the GROUP, and the [1] or [2] finds the POSITION or VOWEL, respectivly, for each "element". With respect to the -1, unfortunatly, I can't explain why the code looks, apparently, at the place BEFORE each particular instance, which is what the -1 seems to do, because even though it works, and does NOT work if the -1 is removed, I don't understand why. :( Dan Moyer
6. Re: Newbie question. Difficulty understanding a few things.
- Posted by Dan Moyer <danielmoyer at pro?igy.n?t> May 25, 2008
- 682 views
Rob H. wrote: > > > I understand what seek is doing. However. The determines > where seek is currently "looking" because it's incrementing with the for > element > loop. > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > In the Reference Manual, seek only takes one argument... I'm wondering what > the [1] and -1 specifically do. Rob, I could have been more succinct: there IS only one argument being passed, it's what ever is found at the FIRST place in seek_positions[element], minus 1. The counter "element" is looking each item in a sequence variable which has TWO items per element. Dan
7. Re: Newbie question. Difficulty understanding a few things.
- Posted by don cole <doncole at p?c?ell.net> May 25, 2008
- 688 views
Rob H. wrote: > > > I understand what seek is doing. However. The seek_positions[element] > determines > where seek is currently "looking" because it's incrementing with the for > element > loop. > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > In the Reference Manual, seek only takes one argument... I'm wondering what > the [1] and -1 specifically do.
seek_positions = {} for element = 1 to length(string) do if find(string[element],vowels) then seek_positions = append(seek_positions,{element, string[element]}) string[element] = ' ' end if end for
Here we are building a 2 element sequence called seek_positions. the first element being the element number. the second element being a string (' ') in the code seek_positions[1][1]=1 seek_positions[1][2]=' ' seek_postition[2][1]=2 seek_positions[2][2]=' ' etc... so seek_positions[2][1]-1=1 seek_positions[5][1]-3=2 Don Cole
8. Re: Newbie question. Difficulty understanding a few things.
- Posted by Dan Moyer <danielmoyer at pro?ig?.net> May 25, 2008
- 690 views
don cole wrote: > > Rob H. wrote: > > > > > > I understand what seek is doing. However. The seek_positions[element] > > determines > > where seek is currently "looking" because it's incrementing with the for > > element > > loop. > > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > > In the Reference Manual, seek only takes one argument... I'm wondering what > > the [1] and -1 specifically do. > }}} <eucode> > seek_positions = {} > for element = 1 to length(string) do > if find(string[element],vowels) then > seek_positions = append(seek_positions,{element, string[element]}) > string[element] = ' ' > end if > end for > </eucode> {{{ > > Here we are building a 2 element sequence called seek_positions. > > the first element being the element number. > > the second element being a string (' ') > > in the code > seek_positions[1][1]=1 > seek_positions[1][2]=' ' > > seek_postition[2][1]=2 > seek_positions[2][2]=' ' > > etc... > > so seek_positions[2][1]-1=1 > seek_positions[5][1]-3=2 > > Don Cole Hi Don, That's not exactly right, I think. Yes, seek_positions is a two element array, but the empty space ' ' is not being added to seek_positions, but rather is REPLACING a character in string: string[element] = ' ' seek_positions has an item added each time a vowel is found, and that item consists of the position counter ("element") and the vowel itself. Dan
9. Re: Newbie question. Difficulty understanding a few things.
- Posted by Dan Moyer <danielmoyer at prodi??.net> May 25, 2008
- 690 views
Rob H. wrote: > > > I understand what seek is doing. However. The seek_positions[element] > determines > where seek is currently "looking" because it's incrementing with the for > element > loop. > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > In the Reference Manual, seek only takes one argument... I'm wondering what > the [1] and -1 specifically do. Rob, I think I finally understand what the -1 is there for. From the manual for seek: "The initial file position is 0 for files opened for read, write or update". So, since the first position in the file is a ZERO REFERENCE, every instance of position in the file must be one less than the "element" counter, which starts at ONE, not ZERO. Sheesh, took me long enough! Dan
10. Re: Newbie question. Difficulty understanding a few things.
- Posted by Mike777 <anon4321 at gm?il?com> May 25, 2008
- 687 views
Rob H. wrote: > > > I understand what seek is doing. However. The seek_positions[element] > determines > where seek is currently "looking" because it's incrementing with the for > element > loop. > So why is the [1] necessary?? Not sure if I'm explaining myself correctly... > In the Reference Manual, seek only takes one argument... I'm wondering what > the [1] and -1 specifically do. Me thinks that you didn't read my entire message closely. Go back to the part where I talked about the construct you are looking for clarification about being the equivalent to an array which would be represented by: "seek_positions(element,1)" You really need to read up on sequences and how they are shown. For example: seek_positions[element][1] is the Euphoria way of representing something which is probably more familiar to you as: seek_positions(element,1) Sequences are essentially very flexible arrays where each individual element can be either a number or another (embedded) sequence. This can get very complicated, very quickly. It is also amazingly powerful. But if you think of them as nothing more than arrays, you will begin to understand them. Let's go the other direction. Say you have a 5 by 5 array, so you have 25 elements (and let's make it a 1-based array rather than a zero-based array, for convenience) The first element would be array(1,1) and the last would be array(5,5). They would be shown as: array[1][1] and array[5][5] in Euphoria. So, the [1] that you are asking about is nothing more than the array index for the second element of a two-element index of the seek_positions variable (which in Euphoria is a sequence but in other languages would be called an array). As far as what the -1 does, that says "subtract 1" from the result you determined by looking at the specific element within the seek_positions sequence found at the index of {element,1). It might be helpful if you say what computer language you have the most experience with. That way, people here can make reference to that language when explaining Euphoria's methods. If this isn't clear, come back at us again. Mike
11. Re: Newbie question. Difficulty understanding a few things.
- Posted by don cole <doncole at ?acbell?net> May 25, 2008
- 745 views
Dan Moyer wrote: > > don cole wrote: > > > > Rob H. wrote: > > > > > > > > > I understand what seek is doing. However. The seek_positions[element] > > > determines > > > where seek is currently "looking" because it's incrementing with the for > > > element > > > loop. > > > So why is the [1] necessary?? Not sure if I'm explaining myself > > > correctly... > > > In the Reference Manual, seek only takes one argument... I'm wondering > > > what > > > the [1] and -1 specifically do. > > }}} <eucode> > > seek_positions = {} > > for element = 1 to length(string) do > > if find(string[element],vowels) then > > seek_positions = append(seek_positions,{element, string[element]}) > > string[element] = ' ' > > end if > > end for > > </eucode> {{{ > > > > Here we are building a 2 element sequence called seek_positions. > > > > the first element being the element number. > > > > the second element being a string (' ') > > > > in the code > > seek_positions[1][1]=1 > > seek_positions[1][2]=' ' > > > > seek_postition[2][1]=2 > > seek_positions[2][2]=' ' > > > > etc... > > > > so seek_positions[2][1]-1=1 > > seek_positions[5][1]-3=2 > > > > Don Cole > > Hi Don, > > That's not exactly right, I think. > > Yes, seek_positions is a two element array, but the empty space ' ' is > not being added to seek_positions, but rather is REPLACING a character in > string: string[element] = ' ' > > seek_positions has an item added each time a vowel is found, and that item > consists of the position counter ("element") and the vowel itself. > > Dan yeah! your right I missed that if find there. Don Cole
12. Re: Newbie question. Difficulty understanding a few things.
- Posted by c.k.lester <euphoric at ?kl?ster.com> May 25, 2008
- 691 views
Mike777 wrote: > Rob H. wrote: > > So why is the [1] necessary?? > You really need to read up on sequences and how they are shown. This page might help: http://www.usingeuphoria.com/books/jubilation/?variables
13. Re: Newbie question. Difficulty understanding a few things.
- Posted by Rob H. <robert.hrosc at gmail.?om> May 25, 2008
- 726 views
Thank you everybody for the help. I do understand every demo program in the tutorial thus far. I refuse to move onto other demo programs until I can fully understand what is happening in the code. This will make for some healthy reading later. I'm not familiar with anything except c++. It's a very limited familiarity too. Logic, in general I understand, it's just the syntax that throws me for a loop and how given data is manipulated or referenced by use of said syntax. I'll definitely be posting back... hopefully not too often ;)! Thanks again for the detailed answers.