Shop OBEX P1 Docs P2 Docs Learn Events
Index and length must refer to a location within the string. — Parallax Forums

Index and length must refer to a location within the string.

youngdaveyoungdave Posts: 70
edited 2009-08-28 23:19 in Propeller 1
Dear All

I often get the following annoying error with the code (in ViewPort) below. Sometimes the code works fine, and sometimes I get this error.
What am I doing wrong?

Index and length must refer to a location within the string.
Parameter name: length
Debugger USER INTERFACE


The code is as follows:

=PUB Cull_excess_population | n
=
= repeat while Current_Population_Size > max_population_size
= n := || rr.random // Current_Population_Size + 1
= repeat while n < Current_Population_Size
= Chromosome[noparse][[/noparse]n] := Chromosome[noparse][[/noparse]n+1]
= n++
= Current_Population_Size --

TIA David Young

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-08-28 16:29
    Dave,

    You've really got to start posting your programs between [noparse][[/noparse]code] and [noparse][[/noparse]/code] tags, at the very least; or use the code formatter at www.phipi.com/format. Without doing so, your indentations disappear.

    -Phil
  • youngdaveyoungdave Posts: 70
    edited 2009-08-28 19:36
    .............. posting your programs between
    and
    

    tags ................

    Not everyone knows what you're talking about! Can't be too hard to give an example can it?

    TIA David Young
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-08-28 20:07
    Your last post is all the example you need! smile.gif 'See what those tags did for you?

    If it's still too confusing, just read the forum help page here: forums.parallax.com/forums/help.aspx. Look under the mcode reference at Code.

    -Phil
  • youngdaveyoungdave Posts: 70
    edited 2009-08-28 22:44
    Dear All

    Hope the formatting's OK.

    I often get the following annoying error with the code (in ViewPort) below. Sometimes the code works fine, and sometimes I get this error.
    What am I doing wrong?

    Index and length must refer to a location within the string.
    Parameter name: length
    Debugger USER INTERFACE


    The code is as follows:

    PUB Cull_excess_population | n
     
      repeat while Current_Population_Size > max_population_size
        n := || rr.random // Current_Population_Size + 1
        repeat while n < Current_Population_Size
          Chromosome[noparse][[/noparse]n] := Chromosome[noparse][[/noparse]n+1]
          n++
        Current_Population_Size --  
    
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-08-28 22:56
    Dave,

    I'm not familiar with ViewPort, but it looks from your code that you're assuming that array indexing is 1-based. In Spin, arrays begin with subscript 0. So I think what you want is this:

    PUB Cull_excess_population | n
     
      repeat while Current_Population_Size > max_population_size
        n := || rr.random // Current_Population_Size
        repeat while n + 1 < Current_Population_Size
          Chromosome[noparse][[/noparse]n] := Chromosome[noparse][[/noparse]n+1]
          n++
        Current_Population_Size --
    
    
    


    Also, you can save a lot of compute time if you use longmove (assuming your chromosomes are longs), instead of the repeat loop.

    -Phil
  • youngdaveyoungdave Posts: 70
    edited 2009-08-28 23:19
    Phil
    Thanks. I'll work on that.
    TIA David Young
Sign In or Register to comment.