Shop OBEX P1 Docs P2 Docs Learn Events
Initializing arrays — Parallax Forums

Initializing arrays

CarlosFandangoCarlosFandango Posts: 67
edited 2008-10-02 22:29 in Propeller 1
I've been looking through the manual and the forum, but I can't seem to find an answer to this simple question.

Is it possible to initialise all array elements to a given value in one go by using a range specification of some sort, or do they all have to be initialized individually? Obviously a loop for large arrays makes sense but I was hoping for something like my_array[noparse][[/noparse]0..3] := 0 rather than the lengthier alternatives.

-CF

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-02 14:21
    You can't use the range specifier for arrays, but there is a memory fill statement that works very nicely. Look in the Propeller Manual under BYTEFILL, WORDFILL, and LONGFILL for a description and examples.

    In your example, if my_array was declared as long, you could write: LONGFILL(@my_array,0,4)
  • CarlosFandangoCarlosFandango Posts: 67
    edited 2008-10-02 14:45
    Mike, thanks for that - I had seen the bytefill statement but it didn't click.
  • mparkmpark Posts: 1,305
    edited 2008-10-02 14:58
    Of course if your array is a VAR then it's already initialized to all zeros.

    You might also consider using DAT for your array:

    DAT
    my_array long 0
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-10-02 20:35
    Even if you use BYTEFILL, it doesn't initiaize the array "in one go". It's just that the BYTEFILL code does the work for you and takes a finite amount of time in the process. If you want to initialize an array in one go, put it in the DAT section and list all of the values explicitly there. As soon as your code is loaded, your array is initialized.

    I vaguely remember reading about a syntax that allows you to specify multiple instances of data in DAT like so:
    my_array long 0[noparse][[/noparse]256]
    
    


    but I could be mistaken.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • mparkmpark Posts: 1,305
    edited 2008-10-02 22:29
    D'oh. That's what I was trying to show but the "[noparse][[/noparse] 4 ]" I had got eaten:
    DAT
    my_array long 0[noparse][[/noparse]4]
    
    
Sign In or Register to comment.