1. 0-based Indexing
- Posted by "C. K. Lester" <cklester at yahoo.com> Jul 16, 2003
- 576 views
I don't get it. Why do some languages use 0-based indexing? It's got to be the dumbest thing in programming language history. Or maybe not.
2. Re: 0-based Indexing
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 16, 2003
- 513 views
----- Original Message ----- From: "C. K. Lester" <cklester at yahoo.com> To: "EUforum" <EUforum at topica.com> Subject: 0-based Indexing > > > I don't get it. Why do some languages use 0-based indexing? It's got to be > the dumbest thing in programming language history. Or maybe not. > Well, not so dumb. It really is a performance issue for CPUs. It comes from the world of RAM addresses. The index is actually an offset from an address. In other words, what number do you need to add to an address, to get to the start of the first byte that the address references - the answer is zero. Thus the 'index' to the first byte is zero, to the second byte is one, etc... Although this becomes second nature to mathamatically inclined people, it is not so intutitve for regular people's way of thinking. That is, the first element is 'named' #1, the second is named #2, etc ... -- Derek
3. Re: 0-based Indexing
- Posted by Greg Haberek <g.haberek at comcast.net> Jul 16, 2003
- 541 views
> I don't get it. Why do some languages use 0-based indexing? It's got to be > the dumbest thing in programming language history. Or maybe not. It actually makes a lot of sense. For example, have you ever used structures? The first element is at the address of the structure. Therefore each item would be Address + n - 1 (given that each element is 1 byte) this would cause the first element to be Address + 0, the second to be Address + 1, etc.. This is a very crude example, but I think you get the point. Get a copy of the old Win32Lib by David Cuny, and rip it apart, you'll see the way he has structures set up. The new Win32Lib uses a more automated system for stuctures so you never see any numbers. The reason Euphoria uses 1-based indexing is to make it easier to read and understand. In some languages you can even set the minimum base, like in Visual Basic, with the command "Option Base 1" (i think, it's been a while since I've used VB). ~Greg
4. Re: 0-based Indexing
- Posted by jbrown105 at speedymail.org Jul 16, 2003
- 536 views
I wonder how many ppl will balk at the explanation given below ;] jbrown On Wed, Jul 16, 2003 at 04:04:08PM -0500, C. K. Lester wrote: > > > I don't get it. Why do some languages use 0-based indexing? It's got to be > the dumbest thing in programming language history. Or maybe not. > Its because of the fact that (*cough* normal) computers count starting at zero. It is also related to the way C does things ... for an array void_pointer, void_pointer[i] is the same as void_pointer+(sizeof(*void_pointer)*i)... to be a bit clearer, void_pointer[i] would be the same as taking the number of bytes that one element of void_pointer would take up, and then multipling that by i, which would give the number of bytes that i elements would take up (this number is needed to determine where the element is located relative to the start of the array). So, if void_pointer is an array of int's, i is 5, and sizeof(int) is 4, then the 6th element (remember, we start counting at zero here) is sizeof(int)*i or 4*5 or 20 bytes away from the begining of the array. The final step is to add the value held in void_pointer to what we got above, this gives us an absolute memory address which we can then access like a normal variable. (Remember that in C an array is merely a constant pointer.) Using this scheme, void_pointer[0] = void_pointer+(4*0) = void_pointer, which makes the address held in void_pointer to be the very first element in our array. (This is probably outdated by languages that use more advanced techniques to store its data.) I have heard that the technical reason that sequences in Euphoria start at 1 is because in the C array that is used internally, element 0 is used to hold the length of the sequence. Not sure if this is true or not. jbrown > > > TOPICA - Start your own email discussion group. FREE! > > -- /"\ ASCII ribbon | http://www.geocities.com/jbrown1050/ \ / campain against | Linux User:190064 X HTML in e-mail and | Linux Machine:84163 /*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
5. Re: 0-based Indexing
- Posted by "C. K. Lester" <cklester at yahoo.com> Jul 16, 2003
- 508 views
Derek splained (snippage occurred): > > I don't get it. Why do some languages use 0-based indexing? It's got to be > > the dumbest thing in programming language history. Or maybe not. > > > The index is actually an offset from an address. Okay, I get that... and that's reasonable for the behind-the-scenes data manipulation, but why not hide that detail and use 1-based indexing? Is it that much of a performance issue? Like Greg said, 1-based indexing is more intuitive, easier to read and understand...
6. Re: 0-based Indexing
- Posted by jbrown105 at speedymail.org Jul 16, 2003
- 537 views
On Wed, Jul 16, 2003 at 04:36:18PM -0500, C. K. Lester wrote: > Okay, I get that... and that's reasonable for the behind-the-scenes data > manipulation, but why not hide that detail and use 1-based indexing? Is it > that much of a performance issue? Like Greg said, 1-based indexing is more > intuitive, easier to read and understand... "Behind-the-scenes" ? I guess that would depend on how high level the language was. I.e. I wouldn't expect an assembly language to do that "behind the scenes". If you mean something as high level as Euphoria or Python tho, then I guess that sort of idea would make more sense. jbrown > > > > TOPICA - Start your own email discussion group. FREE! > > -- /"\ ASCII ribbon | http://www.geocities.com/jbrown1050/ \ / campain against | Linux User:190064 X HTML in e-mail and | Linux Machine:84163 /*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
7. Re: 0-based Indexing
- Posted by gertie at visionsix.com Jul 17, 2003
- 538 views
On 16 Jul 2003, at 17:26, jbrown105 at speedymail.org wrote: > > > I wonder how many ppl will balk at the explanation given below ;] > jbrown > > On Wed, Jul 16, 2003 at 04:04:08PM -0500, C. K. Lester wrote: > > > > > > I don't get it. Why do some languages use 0-based indexing? It's got to be > > the > > dumbest thing in programming language history. Or maybe not. > > > > Its because of the fact that (*cough* normal) computers count starting at > zero. > > It is also related to the way C does things ... for an array void_pointer, > void_pointer[i] is the same as void_pointer+(sizeof(*void_pointer)*i)... > to be a bit clearer, void_pointer[i] would be the same as taking the number of > bytes that one element of void_pointer would take up, and then multipling that > by i, which would give the number of bytes that i elements would take up (this > number is needed to determine where the element is located relative to the > start > of the array). > > So, if void_pointer is an array of int's, i is 5, and sizeof(int) is 4, then > the > 6th element (remember, we start counting at zero here) is sizeof(int)*i or 4*5 > or 20 bytes away from the begining of the array. > > The final step is to add the value held in void_pointer to what we got above, > this gives us an absolute memory address which we can then access like a > normal > variable. (Remember that in C an array is merely a constant pointer.) > > Using this scheme, void_pointer[0] = void_pointer+(4*0) = void_pointer, > which makes the address held in void_pointer to be the very first > element in our array. (This is probably outdated by languages that use more > advanced techniques to store its data.) > > I have heard that the technical reason that sequences in Euphoria start at > 1 is because in the C array that is used internally, element 0 is used to hold > the length of the sequence. Not sure if this is true or not. BASIC and Pascal used the first byte as length. (It feels odd that RobC would use an example from Basic, and still not implement CASE and GOTO.) So the string storage data did actually start at zero offset, but not the string itself. Naturally my gripe with Turbo Pascal is that it began a move to C strings, so one could not store a null in byte in the string. But then, DOS frowned on that too. Kat
8. Re: 0-based Indexing
- Posted by Lucius Hilley <l3euphoria at bellsouth.net> Jul 17, 2003
- 531 views
I have suffered several times from Euphoria's lack of 0-based indexing. Adding and Subtracting 1's to make things Offset into the correct location. 1-based indexing is a real pain in the neck. Only good for the feeble minded that can't grasp the power of the dark side. eh, um. Wait. The force. Luke, Good guys, Obe one. I'm good. I swear. Really, I am. I'm one of the good guys. You can believe me because you have my word on it and I'm a good guy, so, you can believe me. Oh no, I'm starting to sound like Strong Bad. Crap. That cartoon has like, freaking corrupted me man. Help!!!!! Save me!!! Lucius L. Hilley III ----- Original Message ----- From: "C. K. Lester" <cklester at yahoo.com> To: "EUforum" <EUforum at topica.com> Sent: Wednesday, July 16, 2003 05:04 PM Subject: 0-based Indexing > > > I don't get it. Why do some languages use 0-based indexing? It's got to be > the dumbest thing in programming language history. Or maybe not. > > > > TOPICA - Start your own email discussion group. FREE! > >
9. Re: 0-based Indexing
- Posted by dm31 at uow.edu.au Jul 17, 2003
- 519 views
>From: "C. K. Lester" <cklester at yahoo.com> >Derek splained (snippage occurred): > >> > I don't get it. Why do some languages use 0-based indexing? It's got to >be >> > the dumbest thing in programming language history. Or maybe not. >> > >> The index is actually an offset from an address. > >Okay, I get that... and that's reasonable for the behind-the- scenes data >manipulation, but why not hide that detail and use 1-based indexing? Is it >that much of a performance issue? Like Greg said, 1-based indexing is more >intuitive, easier to read and understand... Well, for me personally (maybe because I'm use to programming in C/C++/ASM) 0-based is easier and more understandable. Also, for example. say I want to use 1 byte of storage as my index var. If I start at 0, I get from 0-255 different elements, whereas if I used 1-base then I could only get 1- 255. Having 0-based makes some algo's cleaner to write, and more effiecent. (in above example use a 256 length array, I only need 1 byte for 0-based, and 2 bytes for 1-based. Not much of a difference, but I'm a large program with many of those it would) Cheers, Dan
10. Re: 0-based Indexing
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 17, 2003
- 531 views
On Thu, 17 Jul 2003 12:33:38 +1000 (07/17/03 12:33:38) , <dm31 at uow.edu.au> wrote: > > >> From: "C. K. Lester" <cklester at yahoo.com> > >> Derek splained (snippage occurred): >> >>> > I don't get it. Why do some languages use 0-based > indexing? It's got to >> be >>> > the dumbest thing in programming language history. Or > maybe not. >>> > >>> The index is actually an offset from an address. >> >> Okay, I get that... and that's reasonable for the behind-the- > scenes data >> manipulation, but why not hide that detail and use 1-based > indexing? Is it >> that much of a performance issue? Like Greg said, 1-based > indexing is more >> intuitive, easier to read and understand... > > Well, for me personally (maybe because I'm use to programming in > C/C++/ASM) 0-based is easier and more understandable. Also, for example. > say I want to use 1 byte of storage as my index var. If I start at 0, I > get from 0-255 different elements, whereas if I used 1-base then I could > only get 1- > 255. Spoken like a true C coder. This is Euphoria - we don't have byte, words and doublewords any more, just atom and sequences. Using an atom (the smallest unit) we can access billions of elements, so losing one possible value is not so tough. Also, an index with a value of zero now has meaning in Euphoria; its not got a usable value in it. > Having 0-based makes some algo's cleaner to write, and more effiecent. > (in above example use a 256 length array, I only need 1 byte for 0-based, > and 2 bytes for 1-based. Not much of a difference, but I'm a large > program with many of those it would) How many are really needed to 'make a difference'? Hundreds, Thousands, Millions? Typically, a complex program might have a few thousand variables, so lets say something like 5000 simultaneously used index variables. Comparing a 1-byte per index to Euphoria's atoms, we could save 15000 bytes of RAM ( 0.14% of a megabyte) - hmmmmm not too much nowadays. Not to mention that on average, modern CPUs process 32-bit integers faster than 8- bit integers due to accessing 32-bit aligned data faster than off-aligned data. As for algorithms, I agree that some algorithms are better suited to offsets (C-indexes) rather than ordinals (Eu-indexes). And some aren't. Also, some algorithms work better with ordinals than offsets. -- A method of removing one sequence from another. -- Euphoria's 1-based indexing n = find(a,b) if n then b = b[1..n-1] & a[n + length(a) - 1 .. length(b)] end if -- C's 0-based indexing n = find(a,b) if n >= 0 then b = b[0..n-1] & a[n + length(a) .. length(b) - 1] end if As you can see, not much of a difference really. However, the language called 'D' has slicing too, but its slices exclude the last element referenced. Thus in D (which is a zero-based index system, BTW) you would code... n = find(a,b) if n >= 0 then b = b[0..n] & a[n + length(a) .. length(b)] end if which is a difference. The upshot is, work with the language you've got. One can still always find room for improvements though -- cheers, Derek Parnell
11. Re: 0-based Indexing
- Posted by David Cuny <dcuny at LANSET.COM> Jul 17, 2003
- 520 views
Greg wrote: > In some languages you can even set the minimum base, like in > Visual Basic, with the command "Option Base 1" (i think, it's been a while > since I've used VB). You used to be able to set whatever lower index you wanted in VB - even a value like -23. Now, with the advent of VB.NET, all arrays start at zero. Bleah. -- David Cuny
12. Re: 0-based Indexing
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jul 17, 2003
- 519 views
On Wed, 16 Jul 2003 16:36:18 -0500, "C. K. Lester" <cklester at yahoo.com> wrote: > > >Derek splained (snippage occurred): > >> > I don't get it. Why do some languages use 0-based indexing? It's got= to >be >> > the dumbest thing in programming language history. Or maybe not. >> > >> The index is actually an offset from an address. > >Okay, I get that... and that's reasonable for the behind-the-scenes data >manipulation, but why not hide that detail and use 1-based indexing? Is = it >that much of a performance issue? Like Greg said, 1-based indexing is = more >intuitive, easier to read and understand... > I'm with you on this one, apart from: What performance issue? If an array starts at say #6A8, and each element is say 4 bytes, then 0-style you find the i'th element by calculating #6A8+(i*4). On 1-style you find the i'th element naively by #6A8+((i-1)*4), ...OR - Just pre-calculate the base address and do #6A4+(i*4)... oh, look, no overhead whatsoever! I believe, way back when, someone guffed big time thinking this would help. They were wrong, in my opinion. It possibly makes sense if the array starts near #0, but C (and assembly) gleefully ignore carry and overflow bits, so I don't see the problem with that either. Euphoria Rocks! Pete
13. Re: 0-based Indexing
- Posted by Christian.CUVIER at agriculture.gouv.fr Jul 17, 2003
- 521 views
> From: gertie at visionsix.com > Subject: Re: 0-based Indexing > > > On 16 Jul 2003, at 17:26, jbrown105 at speedymail.org wrote: > > >> I wonder how many ppl will balk at the explanation given below ;] >> jbrown >> >> On Wed, Jul 16, 2003 at 04:04:08PM -0500, C. K. Lester wrote: > >> > >> > I don't get it. Why do some languages use 0-based indexing? It's got to be >> > the >> > dumbest thing in programming language history. Or maybe not. >> > > >> Its because of the fact that (*cough* normal) computers count starting at >> zero. >> >> It is also related to the way C does things ... for an array void_pointer, >> void_pointer[i] is the same as void_pointer+(sizeof(*void_pointer)*i)... >> to be a bit clearer, void_pointer[i] would be the same as taking the number >> of >> bytes that one element of void_pointer would take up, and then multipling >> that >> by i, which would give the number of bytes that i elements would take up >> (this >> number is needed to determine where the element is located relative to the >> start >> of the array). >> >> So, if void_pointer is an array of int's, i is 5, and sizeof(int) is 4, then >> the >> 6th element (remember, we start counting at zero here) is sizeof(int)*i or >> 4*5 >> or 20 bytes away from the begining of the array. >> >> The final step is to add the value held in void_pointer to what we got above, >> this gives us an absolute memory address which we can then access like a >> normal >> variable. (Remember that in C an array is merely a constant pointer.) >> >> Using this scheme, void_pointer[0] = void_pointer+(4*0) = void_pointer, >> which makes the address held in void_pointer to be the very first >> element in our array. (This is probably outdated by languages that use more >> advanced techniques to store its data.) >> >> I have heard that the technical reason that sequences in Euphoria start at >> 1 is because in the C array that is used internally, element 0 is used to >> hold >> the length of the sequence. Not sure if this is true or not. > > > BASIC and Pascal used the first byte as length. (It feels odd that RobC > would use an example from Basic, and still not implement CASE and > GOTO.) So the string storage data did actually start at zero offset, but not > the string itself. Naturally my gripe with Turbo Pascal is that it began a > move > to C strings, so one could not store a null in byte in the string. But then, > DOS frowned on that too. > > Kat In which way does it frown? The basic display functions (int 21/09 and BIOS int 10/0E and 13) will happily print nulls, provided the current codepage supports them. Besides, for all the DOS calls I know which take an ASCIZ string as pointed argument, that arguent is a file, path or machine name, where #0-#1F are not allowed or either relevant. Or did I overlook anything? CChris
14. Re: 0-based Indexing
- Posted by Mike Nelson <MichaelANelson at WORLDNET.ATT.NET> Jul 17, 2003
- 526 views
Greg Haberek wrote: <snip> > It actually makes a lot of sense. For example, have you ever used > The reason Euphoria uses 1-based indexing is to make it easier to read and > understand. In some languages you can even set the minimum base, like in > Visual Basic, with the command "Option Base 1" (i think, it's been a while > since I've used VB). VB 6.0 defaults to base 0 if Option Base is not used; also you can set lower and upper bounds: Dim YearData(1970 to 2010) as Integer This is rather useful, as you can use 0-based, 1-based, or some other base, whichever suits the algorithm at hand. But I guess M$ doesn't like giving the programmer that much freedom--VB.NET uses 0-based indexing exclusively. -- Mike Nelson (I do VB for cash and Eu for love.)
15. Re: 0-based Indexing
- Posted by gertie at visionsix.com Jul 17, 2003
- 537 views
On 17 Jul 2003, at 12:34, Christian.CUVIER at agriculture.gouv.fr wrote: > > > > Date: Wed, 16 Jul 2003 19:16:35 -0500 > > From: gertie at visionsix.com <snip> > > BASIC and Pascal used the first byte as length. (It feels odd that RobC > > would use an example from Basic, and still not implement CASE and > > GOTO.) So the string storage data did actually start at zero offset, but not > > the string itself. Naturally my gripe with Turbo Pascal is that it began a > > move to C strings, so one could not store a null in byte in the string. But > > then, DOS frowned on that too. > > > > Kat > > In which way does it frown? > The basic display functions (int 21/09 and BIOS int 10/0E and 13) will > happily print nulls, provided the current codepage supports them. > Besides, for all the DOS calls I know which take an ASCIZ string as > pointed argument, that arguent is a file, path or machine name, where > #0-#1F are not allowed or either relevant. > Or did I overlook anything? Jeeze, excuse me. In dos4 and using Turbo Pascal on a 386 in 1994(?), when writing a txt file with a null char in it, there wasn't a problem. When reading it back in, dos would truncate the file at the null char. Your mileage may vary. Look, i have met only one other computer programmer in real life, ever, even going back to the Z80/6502/8035 days. I know only one person who even HAS a puter in this state now. So while you guys have puter jobs, and easily make yourself understood because you speak to other programmers daily, WEEKS go by here between times i even SEE another human,,, let alone speak to one. Kat
16. Re: 0-based Indexing
- Posted by Igor Kachan <kinz at peterlink.ru> Jul 17, 2003
- 518 views
Hello CK, > > I have suffered several times from Euphoria's > > lack of 0-based indexing. > > Adding and Subtracting 1's to make things > > Offset into the correct location. > > Uh... the first item of a sequence is s[1]. > How is that at all wrong? > > I understand you're probably talking about > poking things into memory or something else > other than arrays or sequence variables. > > > 1-based indexing is a real pain in the neck. > > Maybe for those who can't understand the > easy-to-understand sequence indexing, Darth. :P The optional 0-indexing in EU may be useful if you want to translate your basic programs into EU code. If you have many basic arrays with 0-indexing and want to translate them into EU, yes, this is a real pain and a rich source of the subtle bugs, especially when indexes are calculated. I have dropped some my translations from basic into EU just because of too many confusions in indexes, especially if some of the 0-elements are or are not empty. Maybe, someone has a powerful tip, other than just a good attention? For the new programming, it is not important, 1 or 0, I think. Regards, Igor Kachan kinz at peterlink.ru
17. Re: 0-based Indexing
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jul 17, 2003
- 532 views
On Thu, 17 Jul 2003 19:43:08 +0400, Igor Kachan <kinz at peterlink.ru> wrote: >I have dropped some my translations from basic into EU=20 >just because of too many confusions in indexes, >especially if some of the 0-elements=20 >are or are not empty. > >Maybe, someone has a powerful tip,=20 >other than just a good attention? I remember porting an array(-(m+1)...(n+1)) to Eu. In my minds eye I replaced all "[" in the input source with "[m+2+" and ".." with "..m+2+". Pete
18. Re: 0-based Indexing
- Posted by jbrown105 at speedymail.org Jul 17, 2003
- 522 views
On Thu, Jul 17, 2003 at 12:33:38PM +1000, dm31 at uow.edu.au wrote: > <snip> > Also, for example. say I want to use 1 byte of storage as my > index var. If I start at 0, I get from 0-255 different > elements, whereas if I used 1-base then I could only get 1- > 255. <snip> Um, in C, int x[255] would give elements 0-254. Not sure what language you are talking about. jbrown -- /"\ ASCII ribbon | http://www.geocities.com/jbrown1050/ \ / campain against | Linux User:190064 X HTML in e-mail and | Linux Machine:84163 /*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
19. Re: 0-based Indexing
- Posted by eugtk at yahoo.com Jul 17, 2003
- 517 views
> Look, i have met only one other computer programmer > in real life, ever, even > going back to the Z80/6502/8035 days. I know only > one person who even > HAS a puter in this state now... Jeez, Kat, even the guy who clears brush from my yard is an MSCE. Perhaps he's just realized that Mother Nature comes up with new bugs far less often that Microsoft does. Irv
20. Re: 0-based Indexing
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 17, 2003
- 509 views
----- Original Message ----- From: <jbrown105 at speedymail.org> To: "EUforum" <EUforum at topica.com> Subject: Re: 0-based Indexing > > > On Thu, Jul 17, 2003 at 12:33:38PM +1000, dm31 at uow.edu.au wrote: > > > <snip> > > Also, for example. say I want to use 1 byte of storage as my > > index var. If I start at 0, I get from 0-255 different > > elements, whereas if I used 1-base then I could only get 1- > > 255. > <snip> > > Um, in C, int x[255] would give elements 0-254. > > Not sure what language you are talking about. Jim, he is not talking about declaring an array with a '255', but using a BYTE to hold the entire set of index values for an array 0-255. To declare such an array he would write "int x[256]" of course. -- Derek
21. Re: 0-based Indexing
- Posted by jbrown105 at speedymail.org Jul 17, 2003
- 516 views
Ok now I understand. This is a pretty good point in favor of 0 based indexing. jbrown On Fri, Jul 18, 2003 at 07:49:03AM +1000, Derek Parnell wrote: > > > ----- Original Message ----- > From: <jbrown105 at speedymail.org> > To: "EUforum" <EUforum at topica.com> > Sent: Friday, July 18, 2003 5:10 AM > Subject: Re: 0-based Indexing > > > > On Thu, Jul 17, 2003 at 12:33:38PM +1000, dm31 at uow.edu.au wrote: > > > > > <snip> > > > Also, for example. say I want to use 1 byte of storage as my > > > index var. If I start at 0, I get from 0-255 different > > > elements, whereas if I used 1-base then I could only get 1- > > > 255. > > <snip> > > > > Um, in C, int x[255] would give elements 0-254. > > > > Not sure what language you are talking about. > > Jim, > he is not talking about declaring an array with a '255', but using a BYTE to > hold the entire set of index values for an array 0-255. To declare such an array > he would write "int x[256]" of course. > -- > Derek > > > > TOPICA - Start your own email discussion group. FREE! > > -- /"\ ASCII ribbon | http://www.geocities.com/jbrown1050/ \ / campain against | Linux User:190064 X HTML in e-mail and | Linux Machine:84163 /*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
22. Re: 0-based Indexing
- Posted by Juergen Luethje <j.lue at gmx.de> Jul 17, 2003
- 505 views
Hi Igor, you wrote: > If you have many basic arrays with 0-indexing and want > to translate them into EU, yes, this is a real pain and > a rich source of the subtle bugs, especially when > indexes are calculated. > > I have dropped some my translations from basic into EU > just because of too many confusions in indexes, > especially if some of the 0-elements > are or are not empty. > > Maybe, someone has a powerful tip, > other than just a good attention? Instead of creating, writing, and reading the arrays/sequences directly, you can access them via functions. Of course, you have the full power and flexibility of Euphoria. Here is just an example: -----------------------------[ begin code ]----------------------------- constant LBOUND = 1, UBOUND = 2 global function array_dim (sequence bounds, object initVal) -- create an one-dimensional 'array' with arbitrary bounds -- (even negative indexes are allowed) integer lBound, uBound lBound = bounds[LBOUND] uBound = bounds[UBOUND] if lBound > uBound then return -1 -- error end if return {lBound, uBound, repeat(initVal, uBound-lBound+1)} end function global function array_put (sequence array, integer index, object val) -- write to an one-dimensional 'array' with arbitrary bounds integer lBound lBound = array[LBOUND] if index < lBound or array[UBOUND] < index then return -1 -- error end if array[3][index-lBound+1] = val return array end function global function array_get (sequence array, integer index) -- read from an one-dimensional 'array' with arbitrary bounds integer lBound lBound = array[LBOUND] if index < lBound or array[UBOUND] < index then return -1 -- error end if return array[3][index-lBound+1] end function ---+++ demo +++--- include misc.e sequence who who = array_dim({2000, 2002}, "") puts(1, "Our 'array':\n") pretty_print(1, who, {3}) who = array_put(who, 2000, "Kim Dae-jung") who = array_put(who, 2001, "United Nations, Kofi Annan") who = array_put(who, 2002, "Jimmy Carter") puts(1, "\nRecent Nobel Prices for Peace:\n") for year = 2000 to 2002 do printf(1, "%d: %s\n", {year, array_get(who, year)}) end for ------------------------------[ end code ]------------------------------ In August 2002, there was also a discussion about arrays on this list. Derek provided code for creating (1-based) multi-dimensional 'arrays', that looked like this: -----------------------------[ begin code ]----------------------------- global function dim_array (sequence dimension, object init_value) -- example: sequence a -- a = dim_array({3,7,4}, 0) object array if length(dimension) = 0 then return 0 -- error end if array = init_value for i = length(dimension) to 1 by -1 do if atom(dimension[i]) and dimension[i] > 0 then array = repeat(array, dimension[i]) else return i -- error end if end for return array end function ------------------------------[ end code ]------------------------------ If you combine both approaches, you'll have code for handling multi-dimensional arrays with (almost) arbitrary bounds. Best regards, Juergen -- /"\ ASCII ribbon campain | |\ _,,,---,,_ \ / against HTML in | /,`.-'`' -. ;-;;,_ X e-mail and news, | |,4- ) )-,_..;\ ( `'-' / \ and unneeded MIME | '---''(_/--' `-'\_)
23. Re: 0-based Indexing
- Posted by Juergen Luethje <j.lue at gmx.de> Jul 17, 2003
- 515 views
Hi Mike, you wrote: > VB 6.0 defaults to base 0 if Option Base is not used; After my understanding, 'Option Base' is a global variable, and it's value also affects code in include files, right? If so, this is "poison" regarding modular programming, I think. > also you can set lower and upper bounds: > > Dim YearData(1970 to 2010) as Integer > > This is rather useful, as you can use 0-based, 1-based, or some other base, > whichever suits the algorithm at hand. Full agreement. Best regards, Juergen -- /"\ ASCII ribbon campain | |\ _,,,---,,_ \ / against HTML in | /,`.-'`' -. ;-;;,_ X e-mail and news, | |,4- ) )-,_..;\ ( `'-' / \ and unneeded MIME | '---''(_/--' `-'\_)
24. Re: 0-based Indexing
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jul 17, 2003
- 512 views
On Thu, 17 Jul 2003 18:20:50 -0400, jbrown105 at speedymail.org wrote: >Ok now I understand. > >This is a pretty good point in favor of 0 based indexing. but as previously stated, you still need an extreme situation to cost 0.14% (or whatever was quoted) of one megabyte Pete
25. Re: 0-based Indexing
- Posted by dm31 at uow.edu.au Jul 18, 2003
- 515 views
>>Ok now I understand. >>This is a pretty good point in favor of 0 based indexing. I replied a bit late. DP explained it better :>
26. Re: 0-based Indexing
- Posted by dm31 at uow.edu.au Jul 18, 2003
- 512 views
>On Thu, Jul 17, 2003 at 12:33:38PM +1000, dm31 at uow.edu.au >wrote: >> ><snip> >> Also, for example. say I want to use 1 byte of storage as >my >> index var. If I start at 0, I get from 0-255 different >> elements, whereas if I used 1-base then I could only get 1- >> 255. ><snip> > >Um, in C, int x[255] would give elements 0-254. >Not sure what language you are talking about. > >jbrown Please note that I *DID NOT* say 'int x[255]' I said using a 1byte integer for the *INDEX* eg, unsigned char index long x[256] then using index as the 'index' there is a big difference in what I said, and what you presented :>
27. Re: 0-based Indexing
- Posted by jbrown105 at speedymail.org Jul 18, 2003
- 518 views
On Thu, Jul 17, 2003 at 11:47:05PM +0100, Pete Lomax wrote: > > > On Thu, 17 Jul 2003 18:20:50 -0400, jbrown105 at speedymail.org wrote: > >Ok now I understand. > > > >This is a pretty good point in favor of 0 based indexing. > but as previously stated, you still need an extreme situation to cost > 0.14% (or whatever was quoted) of one megabyte That was not the good point I was referring to. jbrown > > Pete > > > > TOPICA - Start your own email discussion group. FREE! > > -- /"\ ASCII ribbon | http://www.geocities.com/jbrown1050/ \ / campain against | Linux User:190064 X HTML in e-mail and | Linux Machine:84163 /*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
28. Re: 0-based Indexing
- Posted by Igor Kachan <kinz at peterlink.ru> Jul 18, 2003
- 523 views
Hi Juergen, > > Hi Igor, you wrote: > > > If you have many basic arrays with 0-indexing and want > > to translate them into EU, yes, this is a real pain and > > a rich source of the subtle bugs, especially when > > indexes are calculated. > > > > I have dropped some my translations > > from basic into EU just because of too > > many confusions in indexes, > > especially if some of the 0-elements > > are or are not empty. > > > > Maybe, someone has a powerful tip, > > other than just a good attention? > > Instead of creating, writing, and reading the > arrays/sequences directly, you can access them > via functions. Of course, you have the full power > and flexibility of Euphoria. [snipped examples] Yes, I used this way for the first step of translation to get translated from basic program working as expected. But then, step by step, I replaced the functions calls with the direct indexing of sequences to have an optimum effect. And the translation still was not very easy. But the program translated from basic into EU becomes infinitely more flexible and powerful for further improvements. I say about QB4.5 this moment. Regards, Igor Kachan kinz at peterlink.ru
29. Re: 0-based Indexing
- Posted by Igor Kachan <kinz at peterlink.ru> Jul 18, 2003
- 505 views
Hi Pete, ----------- > On Thu, 17 Jul 2003 19:43:08 +0400, Igor Kachan <kinz at peterlink.ru> > wrote: > > >I have dropped some my translations from basic into EU > >just because of too many confusions in indexes, > >especially if some of the 0-elements > >are or are not empty. > > > >Maybe, someone has a powerful tip, > >other than just a good attention? > > I remember porting an array(-(m+1)...(n+1)) to Eu. > In my minds eye I replaced all "[" in the input > source with "[m+2+" > and ".." with "..m+2+". > > Pete Ok, I'll keep in mind this tip. Thanks. Regards, Igor Kachan kinz at peterlink.ru
30. Re: 0-based Indexing
- Posted by Juergen Luethje <j.lue at gmx.de> Jul 18, 2003
- 552 views
Hi Igor, you wrote: > [snipped examples] > > Yes, I used this way for the first step of translation > to get translated from basic program working as expected. > But then, step by step, I replaced the functions calls > with the direct indexing of sequences to have an > optimum effect. > And the translation still was not very easy. The advantage of my previous examples was, that the bounds were stored inside the 'array'. Here is some more sample code, that changes the indexes more directly, while the 'array' itself remains unchanged. -----------------------------[ begin code ]----------------------------- sequence countries integer lBound, uBound, delta global function option_base (integer base) return base-1 end function -- Create a 0-based 'array' / BASIC: dim countries(0:2) lBound = 0 uBound = 2 countries = repeat(0, uBound-lBound+1) delta = option_base(lBound) -- The function option_base() is -- used for better readability. -- Fill the 'array' countries[0-delta] = "Russia" countries[1-delta..2-delta] = {"USA", "France"} -- Show the content puts(1, "Just a list of countries:\n") for i = 0 to 2 do printf(1, "%d: %s\n", {i, countries[i-delta]}) end for -- Oops, maybe I should have been more specific ... delta = option_base(2000) -- changing the base at runtime puts(1, "\nWhere I recently spent my holidays:\n") puts(1, "year country\n") puts(1, "---- -------\n") for year = 2000 to 2002 do printf(1, "%d: %s\n", {year, countries[year-delta]}) end for ------------------------------[ end code ]----------------------------- Regarding 0-based arrays, you can permanetly replace all occurences of '-delta' by '+1'. That's similar to what Pete wrote. Best regards, Juergen -- Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].
31. Re: 0-based Indexing
- Posted by Igor Kachan <kinz at peterlink.ru> Jul 19, 2003
- 534 views
Hi Juergen, > Hi Igor, you wrote: > > > [snipped examples] > > > > Yes, I used this way for the first step of translation > > to get translated from basic program working as expected. > > But then, step by step, I replaced the functions calls > > with the direct indexing of sequences to have an > > optimum effect. > > And the translation still was not very easy. > > The advantage of my previous examples was, that the bounds were stored > inside the 'array'. Here is some more sample code, that changes the > indexes more directly, while the 'array' itself remains unchanged. [snipped examples] Thanks Juergen, you inspire me and I'll translate into EU my old basic programs with too many different arrays again. Regards, Igor Kachan kinz at peterlink.ru