Arrays in PICAXE

Jamster

Senior Member
Just a few functions to allow the PICAXE to use a makeshift 2 dimensional array. I could only find scraps of code so developed this for those who want it.

The array is stored in the scratchpad and has variable length so be carefull when using it along side put/get/etc.

Code:
SYMBOL x=b0   'x location to insert/remove value
SYMBOL y=b1   'y location to insert/remove value
SYMBOL arrayData=b2 'data to insert/data removed/data grabed
SYMBOL temp2=b3  'internal use (use if you want)
SYMBOL width=10  'the width of the array
SYMBOL temp1=w2  'internal use (use if you want)
SYMBOl length=w3  'length of the array

insert:   'overwrite block x,y 
 temp1=width*y
 temp1=temp1+x
 put temp1,arrayData
 return

grab:    'get block x,y
 temp1=width*y
 temp1=temp1+x
 get temp1,arrayData
 return

append:   'add to end
 temp1=length+1
 put length,arrayData
 inc length
 return
 
push:    'add to start
 for temp1=length to 0 step -1
  get temp1,temp2
  temp1=temp1+1
  put temp1,temp2
  temp1=temp1-1
 next temp1
 put 0,arrayData
 inc length
 return

pop:    'remove (+return) fisrt block
 get 0,arraydata
 for temp1=0 to length
  temp1=temp1+1
  get temp1,temp2
  temp1=temp1-1
  put temp1,temp2
 next temp1
 dec length
 return

remove:   'remove (+return) block at x,y
 temp1=width*y
 temp1=temp1+x
 get temp1,arrayData
 for temp1=temp1 to length
  temp1=temp1+1
  get temp1,temp2
  temp1=temp1-1
  put temp1,temp2
 next temp1
 dec length
 return

delete:   'delete (+return) last block
 temp1=length-1
 get temp1,arrayData
 put temp1,0
 dec length
 return
 

westaust55

Moderator
Well done Jamster.

While I may not have a need at this time, having such snippets stored on the forum can be an advantage when the need arises to prevent reinventing the "wheel".
 

Jamster

Senior Member
I don't have a need for it either at this point in time, just keep seeing threads about "are arrays possible" when I search. :)
 

pbaker21

Senior Member
Is it possible to alter the value an item in the lookup table/storage place? So, if I wanted to change the 'b' to a 'z' for example.

lookup b0,("a,b,c,d,e"), b1

Thanks.
 

nick12ab

Senior Member
Is it possible to alter the value an item in the lookup table/storage place? So, if I wanted to change the 'b' to a 'z' for example.

lookup b0,("a,b,c,d,e"), b1

Thanks.
No. Lookup tables are read only. However you can use variables in a lookup table and these variables can be changed at any time (but not by the lookup command).
 
Top