Shop OBEX P1 Docs P2 Docs Learn Events
Arrays and repeat loop — Parallax Forums

Arrays and repeat loop

JacKalJacKal Posts: 4
edited 2009-07-08 10:06 in Propeller 1
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[noparse][[/noparse] 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

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2009-07-08 08:44
    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. [noparse]/noparse]space1space] = [noparse][[/noparse] 1 ] or [noparse][[/noparse]spaceispace] = [noparse][[/noparse] i[color=white]·[/color.
    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[noparse][[/noparse] 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 MuellerNick Mueller Posts: 815
    edited 2009-07-08 08:57
    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[noparse][[/noparse] 0 ] == place;
    
    



    Nick

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

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-07-08 09:02
    *LOL* ... no more replies from me this morning. I seem to be blind.
  • JacKalJacKal Posts: 4
    edited 2009-07-08 09:13
    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[noparse][[/noparse] 0 ] == j
    left_wall[noparse][[/noparse] 1 ] == j
    left_wall[noparse][[/noparse] 2 ] == j
    .
    .
    .
    left_wall[noparse][[/noparse] 63 ] == j

    to this:

    left_wall[noparse][[/noparse] i ] == j

    the i variable should change between 0-63, but I can only read the first value [noparse][[/noparse] 0 ], after that it wont work. if i use the manual method above, it works as it should.
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-07-08 09:20
    > 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
  • JacKalJacKal Posts: 4
    edited 2009-07-08 09:34
    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[noparse][[/noparse] 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 MuellerNick Mueller Posts: 815
    edited 2009-07-08 09:41
    This sounds more like:
    pub find(what) | i
      repeat i from 0 to 63
        if left_wall[noparse][[/noparse] 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
  • JacKalJacKal Posts: 4
    edited 2009-07-08 10:06
    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
Sign In or Register to comment.