[Tutor] I want some help with arrays...

Luke Paireepinart rabidpoobear at gmail.com
Fri May 18 07:03:51 CEST 2007


James Matthews wrote:
> When the CPU writes to the RAM the 0th location can be written to also. When 
> you alter components of a language for example changing the way an list is 
> indexed you have to be careful because you can accidently jump out of 
> bounds!
def indexOneBased(somelist,index):
   return somelist[index-1]

now you can do
indexOneBased([1,2,3,4,5], 5)
and you will get 5.

Obviously the verbosity of this example seems to imply its uselessness,
but what Bob was getting at is that there are situations where you can 
create a list that you index into based upon some arbitrary starting point.
For example, say you had a calendar program that kept track of my 
schedule for days 1 through 31 of this month.
You'd store the schedule of each day in a list starting with the 0th 
element and ending at the 30th element.
Then, whenever I asked you for the 1st element, for example, you'd 
retrieve the 0th element for me, and so on.
The indexing itself isn't changed, merely the interface to it.
Note that you don't actually index the list starting at the 1th element 
(the 2nd one)
you index it starting at the 0th element, but use indexes that begin at 1.

HTH,
-Luke


More information about the Tutor mailing list