Array cell access [help needed].
50 posts • Page 1 of 2 • 1, 2
Array cell access [help needed].
As I know, Phun don't allow you to change one cell of the array, so I tried to make a special function:
Scene.my.a - adress, scene.my.b - new value,scene.my.ram - array, scene.my.copy - copy array.
But it don't work
There is "List index out of bounds" error.
- Code: Select all
scene.my.addr = 0; scene.my.maxr < scene.my.a ? {scene.my.maxr = scene.my.a} : {}; scene.my.copy = scene.my.ram; scene.my.ram = [0]; for(scene.my.a - 1, (addr)=>{scene.my.ram = scene.my.ram ++ scene.my.copy(addr)}); scene.my.ram = scene.my.ram ++ scene.my.b; scene.my.addr = scene.my.a + 1; for(scene.my.maxr, (addr)=>{scene.my.ram = scene.my.ram ++ scene.my.copy(addr)}); scene.my.com = scene.my.com + 1
Scene.my.a - adress, scene.my.b - new value,scene.my.ram - array, scene.my.copy - copy array.
But it don't work
There is "List index out of bounds" error.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
Can you please show us your scene.my.copy function?
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
It isn't function, it is array.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
Ok, I know the reason. The value of scene.my.a - 1 or scene.my.maxr is too big.
Let's make an example:
I have an array like this:
scene.my.array = [0,1,2,3,4]
Now I want to call a value of the array. For example the third:
scene.my.array(2)
The answer will be of course 2.
But you have to watch out. What if you call the 6th or 7th... value? (Remember: we have got 5 values in our array)
scene.my.array(5)
An error code appears that says "List index out of bounds" because you call a value in the array that doesn't exist. In other words the list index (that's 5 here) exceeds the number of values in your array.
Let's make an example:
I have an array like this:
scene.my.array = [0,1,2,3,4]
Now I want to call a value of the array. For example the third:
scene.my.array(2)
The answer will be of course 2.
But you have to watch out. What if you call the 6th or 7th... value? (Remember: we have got 5 values in our array)
scene.my.array(5)
An error code appears that says "List index out of bounds" because you call a value in the array that doesn't exist. In other words the list index (that's 5 here) exceeds the number of values in your array.
Last edited by KarateBrot on Sun Oct 25, 2009 10:11 am, edited 2 times in total.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
No, arrays starts at 0.
scene.my.array(3) = 3
scene.my.array(3) = 3
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
savask wrote:No, arrays starts at 0.
scene.my.array(3) = 3
Oh, yeah of course, thanks. A small mistake but I already corrected it. I have chosen [0,1,2,3,4], starting at 0 to make it more easy so that the list index is the same as the value but all the same I made this silly mistake
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
I tried to use small address value, and big number of cells, but it don't work too.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
did you try it with both of your for-functions?
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
Can't understand exactly what do you mean, but if you about scene.my.copy, so yes, I did.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
for(scene.my.a - 1, (addr)=>{scene.my.ram = scene.my.ram ++ scene.my.copy(addr)});
for(scene.my.maxr, (addr)=>{scene.my.ram = scene.my.ram ++ scene.my.copy(addr)});
It seems like "scene.my.a - 1" or "scene.my.maxr" is too big. You have to keep these variables as low as the number of cells in your scene.my.copy array.
for(scene.my.maxr, (addr)=>{scene.my.ram = scene.my.ram ++ scene.my.copy(addr)});
It seems like "scene.my.a - 1" or "scene.my.maxr" is too big. You have to keep these variables as low as the number of cells in your scene.my.copy array.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
I tried, and it don't work again!
Scene.my.maxr - it is the biggest address used.
Scene.my.maxr - it is the biggest address used.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
Are you sure you did everything correct?
in the console type "scene.my.maxr = 2", "scene.my.a = 2" and "scene.my.ram = [3,5,6,6,4]"
Now test it again.
in the console type "scene.my.maxr = 2", "scene.my.a = 2" and "scene.my.ram = [3,5,6,6,4]"
Now test it again.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
No, this don't too...
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
I don't know what you're doing but I tested your code, too... and it works
After I corrected the "List index out of bounds" error a new error occured that said "++ only add lists" but you can correct this by making [] brackets around "scene.my.copy(addr)". it looks like this:
for(scene.my.a - 1, (addr)=>{scene.my.ram = scene.my.ram ++ [scene.my.copy(addr)]});
for(scene.my.maxr, (addr)=>{scene.my.ram = scene.my.ram ++ [scene.my.copy(addr)]});
But to adjust the values of the variables definitely solves your out of bound error problem.
After I corrected the "List index out of bounds" error a new error occured that said "++ only add lists" but you can correct this by making [] brackets around "scene.my.copy(addr)". it looks like this:
for(scene.my.a - 1, (addr)=>{scene.my.ram = scene.my.ram ++ [scene.my.copy(addr)]});
for(scene.my.maxr, (addr)=>{scene.my.ram = scene.my.ram ++ [scene.my.copy(addr)]});
But to adjust the values of the variables definitely solves your out of bound error problem.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
It work with the brackets, thanks!
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
oh okay so you didn't read the error message that was the problem
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
There are warning messages, but I have found one bug in my algorithm.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
If something is wrong ask again I gladly like to help
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
Are there a limit of number of array cells?
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
I don't know but I just tested it with about 4500 cells and it's still working so if there's a limit at 4920587248974 cells it shouldn't bother you
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
Strange...
I can't use more then 50 cells.
I can't use more then 50 cells.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: Array cell access [help needed].
very strange. what's the error message?
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
For loops can only repeat up to 40 times because they're implemented recursively.
In C:\Program Files\Algodoo\thyme.cfg:
You can only call functions up to 40 levels deep, so this fails for anything higher.
I made a better for loop at http://www.algodoo.com/forum/viewtopic.php?f=13&t=371&p=8695#p8695, but any loop (including this) will be very slow for large numbers of iterations.
In C:\Program Files\Algodoo\thyme.cfg:
- Code: Select all
for = (n, what)=>{
n <= 0 ? false : {
for(n - 1, what);
what(n-1)
}
};
You can only call functions up to 40 levels deep, so this fails for anything higher.
I made a better for loop at http://www.algodoo.com/forum/viewtopic.php?f=13&t=371&p=8695#p8695, but any loop (including this) will be very slow for large numbers of iterations.
Thymechanic
- immibis
- Posts: 87
- Joined: Wed Sep 30, 2009 12:24 am
Re: Array cell access [help needed].
The problem was a different one. It wasn't the loop limit of the for-function, it was an array error. But it's a cool idea to mention it if savask needs deeper for functions and gets the overflow error, too.
Gradyfitz also made an extended loop function here at Tutorial 20. It seems like it's faster but "only" handles up to 490 loops. If you need more than 490 loops yours is very cool because I couldn't find a limit even with about 5000 loops.
Gradyfitz also made an extended loop function here at Tutorial 20. It seems like it's faster but "only" handles up to 490 loops. If you need more than 490 loops yours is very cool because I couldn't find a limit even with about 5000 loops.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Array cell access [help needed].
Yes, immibis was right. This is cause of for-loop limit.
Can you show me some examples of using your for-function?
Can you show me some examples of using your for-function?
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
50 posts • Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 3 guests