PDA

View Full Version : Arrays and repeat loop



JacKal
07-08-2009, 03:57 PM
Hello there,

I'm fairly new to programming and totally new to microprocessors, but as a summer project from school I have started to get to know little bit about propeller.
Currently I am having trouples with arrays and repeat loops.




pub compare| i,j
repeat i from 0 to 63
j := place ' just a public method that returns values between 0 and 8191
if left_wall[ i ] == j
return 1
else
return 0




I just cant figure it out why this wont work. it reads the first value fine, but after that nothing. Is it some limitation on arrays that I cant use repeat loops to shorten my work, or have I misunderstood something?

Any help is greatly appreciated.

Post Edited (JacKal) : 7/8/2009 8:51:15 AM GMT

MagIO2
07-08-2009, 04:44 PM
Please use the # icon in edit message to create a code-block. Then add your code there to avoid the indentation to be lost. Plus ... if you use arrays you have to put spaces between the square brackets and whatever is insite the brackets. Otherwise the forum will replace it as text·modifier. E.G. [space1space] = [ 1 ] or [spaceispace] = [ i·].
Please update your post.

You talk about an array. Where is the array in your code?



pub compare| i,j
repeat i from 0 to 63
j := place ' just a public method that returns values between 0 and 8191
if left_wall[ i ] == j
return 1
else
return 0


If this is what you wrote (including the indentation), I can't see a problem yet. Then you should provide the whole code.

Post Edited (MagIO2) : 7/8/2009 8:53:03 AM GMT

Nick Mueller
07-08-2009, 04:57 PM
Well this code could be quite shortened, because you *always* return after inspecting the first element in the array.

Actually, your code can be shortened to this and still does the same:



PUB Compare
return left_wall[ 0 ] == place;




Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:
YADRO (http://www.yadro.de)

MagIO2
07-08-2009, 05:02 PM
*LOL* ... no more replies from me this morning. I seem to be blind.

JacKal
07-08-2009, 05:13 PM
In my program(game) I'm trying to use compare to check if something is in some exact location.

the problem is that what I am trying to shorten here is this:

left_wall[ 0 ] == j
left_wall[ 1 ] == j
left_wall[ 2 ] == j
.
.
.
left_wall[ 63 ] == j

to this:

left_wall[ i ] == j

the i variable should change between 0-63, but I can only read the first value [ 0 ], after that it wont work. if i use the manual method above, it works as it should.

Nick Mueller
07-08-2009, 05:20 PM
> In my program(game) I'm trying to use compare to check if something is in some exact location.

Yea, but then you should tell your compare what location you want to inspect.
Read: you have to pass the index you want to inspect to compare -> Compare needs a parameter.


Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:
YADRO (http://www.yadro.de)

JacKal
07-08-2009, 05:34 PM
The place method returns value I'm tracking and I'm using temp var j to store that information, then I'm trying to use repeat loop to go through array left_wall[ i ] to check if it has any values that equals to j then depending if match is found it should return 0 or 1. This doesn't work currently, if I use the i variable to shorten code it only reads the first array value and skips the rest.

sorry if I'm not making much sense, but as English isn't my first language, I'm bit struggling to convey the meaning I'm after

Nick Mueller
07-08-2009, 05:41 PM
This sounds more like:



pub find(what) | i
repeat i from 0 to 63
if left_wall[ i ] == what
return 1

return 0




Or maybe you want to know the index? Then you should return that.

Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:
YADRO (http://www.yadro.de)

JacKal
07-08-2009, 06:06 PM
nvm, your code works just fine, i just had c&p problems

thank you !

Post Edited (JacKal) : 7/8/2009 10:11:38 AM GMT