Array cell access [help needed].

About advanced scenes, and the Thyme scripting language used in Algodoo.

Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 8:38 am

As I know, Phun don't allow you to change one cell of the array, so I tried to make a special function:
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 :problem:
There is "List index out of bounds" error.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 9:12 am

Can you please show us your scene.my.copy function?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 9:33 am

It isn't function, it is array.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 10:02 am

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.
Last edited by KarateBrot on Sun Oct 25, 2009 10:11 am, edited 2 times in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 10:09 am

No, arrays starts at 0.
scene.my.array(3) = 3
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 10:13 am

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 :D
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 10:15 am

I tried to use small address value, and big number of cells, but it don't work too.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 10:17 am

did you try it with both of your for-functions?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 10:20 am

Can't understand exactly what do you mean, but if you about scene.my.copy, so yes, I did.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 10:34 am

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 10:39 am

I tried, and it don't work again! :wtf:
Scene.my.maxr - it is the biggest address used.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 10:49 am

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 10:55 am

No, this don't too...
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 11:21 am

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 11:27 am

It work with the brackets, thanks! :clap:
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 12:09 pm

oh okay so you didn't read the error message that was the problem :D
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 12:15 pm

There are warning messages, but I have found one bug in my algorithm.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 1:07 pm

If something is wrong ask again I gladly like to help
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 1:24 pm

Are there a limit of number of array cells?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 1:31 pm

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 :D
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Sun Oct 25, 2009 1:35 pm

Strange...
I can't use more then 50 cells.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Array cell access [help needed].

Postby KarateBrot » Sun Oct 25, 2009 6:58 pm

very strange. what's the error message?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby immibis » Sun Oct 25, 2009 11:44 pm

For loops can only repeat up to 40 times because they're implemented recursively.
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].

Postby KarateBrot » Mon Oct 26, 2009 1:28 am

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. :D
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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Array cell access [help needed].

Postby savask » Mon Oct 26, 2009 10:45 am

Yes, immibis was right. This is cause of for-loop limit.
Can you show me some examples of using your for-function?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Next

Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 3 guests

cron