Sorry for the long title but this one has me stumped.
To start with I was doing really well, I had a problem, looked in the manual for help found some interesting array commands that I thought could help. Looked us some examples on the forum which IanM had kindly placed in the codebase and it worked. as shown below.
Set window off
`create an empty array to put numbers into
dim numberList() as integer
empty array numberList()
`fill the array using insert, array expands as you add additional numbers
for x = 1 to 3
array insert at bottom numberList()
numberList() = x
next x
do
cls
set cursor 10,10
Print "Please press 1, 2 or 3 "
Print
`checks if there is anything in the entry buffer
if entry$()<>""
`if 1,2,3 is pressed it will print if not it ignores pressed key
for x = 1 to 3
if keystate(x+1) or keystate(x+78)
set cursor 10,30
print "Thankyou. The number you selected is ",x
number# = x
newNumber = 1
endif
next x
`clears entry buffer for next loop
clear entry buffer
endif
`print list using array count to see how many numbers in the list.
for x=0 to array count( numberList() )
print " Item "; x; " = "; numberList(x)
next x
wait key
`to work out where selected number is in list so it can be removed and taken to the top of list
if newNumber = 1
for x=0 to array count( numberList() )
if numberList(x) = number#
indexCount = x
endif
next x
`this will delete number from present list and then plac it at the top
if indexCount = 2
`deleting number
array index to top numberList()
next array index numberList()
next array index numberList()
array delete element numberList()
`placing number at top of list
array insert at top numberList()
numberList() = number#
else
if indexCount = 1
array index to top numberList()
next array index numberList()
array delete element numberList()
` placing number at top of list
array insert at top numberList()
numberList() = number#
else number# = 0 and newNumber = 0
endif
endif
endif
loop
This created the interchangeable list I was after. But I got cocky thought this bit of code
if indexCount = 2
`deleting number
array index to top numberList()
next array index numberList()
next array index numberList()
array delete element numberList()
`placing number at top of list
array insert at top numberList()
numberList() = number#
else
if indexCount = 1
array index to top numberList()
next array index numberList()
array delete element numberList()
` placing number at top of list
array insert at top numberList()
numberList() = number#
else number# = 0 and newNumber = 0
endif
endif
was a little untidy and realised I could replace it with
array index to top numberList()
while array index valid( numberList() )
if numberList() = number#
array delete element numberList()
else
next array index numberList()
endif
endwhile
and it still worked which felt good. Then I realised I could remove
for x=0 to array count( numberList() )
if numberList(x) = number#
indexCount = x
endif
next x
because it wasn't needed any longer, leaving me with
Set window off
`create an empty array to put numbers into
dim numberList() as integer
empty array numberList()
`fill the array using insert, array expands as you add additional numbers
for x = 2 to 4
array insert at bottom numberList()
numberList() = x
next x
do
cls
set cursor 10,10
Print "Please press 1, 2 or 3 "
Print
if newNumber = 1
set cursor 10,30
print "Thankyou. The number you selected is ";number#
endif
`checks if there is anything in the entry buffer
if entry$()<>""
`if 1,2,3 is pressed it will print if not it ignores pressed key
for x = 1 to 3
if keystate(x+1) or keystate(x+78)
number# = x
newNumber = 1
endif
next x
`clears entry buffer for next loop
clear entry buffer
endif
`print list using array count to see how many numbers in the list.
for x=0 to array count( numberList() )
print " Item "; x; " = "; numberList(x)
next x
`This will run through the array untill it finds number selected and then delete it
array index to top numberList()
while array index valid( numberList() )
if numberList() = number#
array delete element numberList()
else
next array index numberList()
endif
endwhile
`this will replace selected number to the top of the list
array insert at top numberList()
numberList() = number#
loop
which works perfectly except it adds an unwanted zero at the top of the list. Now as this is a learning exercise can anyone tell me why this has happened.
Thanks
JC