Skip to content

Array Declaration: #37

@SteveJustin1963

Description

@SteveJustin1963

arrays are declared using [ ].
but we cannot declare an empty one as [ ] n ! with a given for later use
we can get the array size with /S
set array size with [ 3 /S ] // not sure whats best here - future feature ?

this works

:A /Ka! /Kb! /Kc!;
:B [a b c] d! ;
:C d0?. d1?. d2?. ;   // or :C d0?/C d1?/C d2?/C  ;
:D 1(A B C) ;

> D 
123
49 50 51
>D 
abc
97 98 99
>
> [ a b c] a! 
> a/S .
3
>

we can use loop counters /i and /j to also control array size and access

also this works

// Define an array of 5 elements and store its address in 'a'
[ 0 0 0 0 0 ] a! // need to initialise each time 

// Loop 5 times to read input and store in the array
5 (
  `Enter a digit: `       // Prompt the user
  /K 48 - n!              // Read a character, convert ASCII digit to number, store in 'n'
  n a /i ?!  /N              // Store 'n' into array at index /i
)

// Loop 5 times to print each element of the array
5 (
  `Value is: `            // Print label
  a /i ? .  /N              // Fetch array element at index /i and print it
)

/////////////

: A [ 0 0 0 0 0 ] a! ; 
: B 5 ( `Enter a digit: `  /K 48 - n!  n a /i ?!  /N ) ;
: C 5 ( `Value is: ` a /i ? .  /N ) ;

// run it
ABC

///////////////////

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions