Shifting data in arrays


#1

1) Give a description of the problem
One of my favorite pistons works most of the time, but it gives me problems a few times a month. I have two other threads trying to resolve it from different angles, but nobody is able to help there it seems. (maybe my request there is too complex) Sooo, I have decided to try to re-write the piston from scratch here.

Can anyone help me re-write lines 35-41? I have a hunch it can all be turned into one single line. Maybe, by streamlining this, it will help out the reliability of my piston (once it gets complex again)

2) What is the expected behavior?
I need each data point in the array to shift to the previous slot.
(move the data in aa[1] to aa[0]… then from aa[2] to aa[1] etc)

3) What is happening/not happening?
It works the way it is coded now, but eventually, I want 70 data points, and my method takes up too much space.


#2

This seems to be working for me (yes I love a loop! :joy:):


#3

Thanks Steven, but I have three questions.
(Sorry, I am fairly new to arrays and loops)

(1) How do the initial numbers get set for counter and countup?

(2) How does the piston know how many data points I want in the array?

(3) How do the numbers reset back to 0 & 1 for the next time it is run?


#4

It doesn’t know how many you want, but it does know how many are there already. You could replace the size(aa) condition with a solid length but if you tried to pull down data from an unwritten array item you would likely get an error.


#6

No problem:

  1. The variables counter and countup are defined at the beginning with an initial value of 0 and 1. Arrays start at 0 so you need counter to begin there too so you address all the item in the array.

  2. The piston doesn’t know how many you want in the array, but it knows how many there are with size(aa). You could replace this for a fixed number though, once the array is full you shouldn’t have any issues.

  3. The counter and countup are local variables which get set/created every time the piston runs, unlike a global variable the local variables don’t maintain their previous values (from my current experience).


#7

Thanks for taking the time to explain a bit. For some reason, I really thought that using $index was the way to solve this. I am really surprised that you didn’t use $index at all.

From what I have seen with variables, if the top (define) section is left blank, and the variable is written to in the execute section, the variables will definitely retain their values until they are over written again.


#8

right. when assigned a value in the define section the variable becomes a constant otherwise it just retains the last value that was set during the execution.

edit: here is another way of doing this thats just slightly more compact:


#9

@WCmore, I wasn’t aware of the $index variable (only really been at this a few weeks). @bangali has provided the best solution.

@bangali, is there further documentation beyond the wiki? I was searching for how to find the length of an array and it took a while, and trial and error, to get to the size function. I expected to use length.


#10

not really. the knowledge is spread out all over this forums. just ask when you run in to something.


#11

Thank you both for your help. I learned a lot in the process.

I really wish there was one page where I could go to learn more about arrays in webCoRE. If anyone comes across one (or makes one themselves) please post a link here.