
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
Get two integers from the user, then create a two-dimensional array where the two dimensions have the sizes given by those numbers, and which can be accessed in the most natural way possible. Write some element of that array, and then output that element. Finally destroy the array if not done by the language itself.

  [ witheach peek ]         is {peek}        (   { p --> x     )
 
  [ dip dup
      witheach [ peek dup ]
    drop ]                  is depack        (   { p --> *     )
 
  [ reverse
    witheach
      [ dip swap poke ] ]   is repack        (   * p --> {     )
 
  [ dup dip
      [ rot dip
        [ depack drop ] ]
    repack ]                is {poke}        ( x { p --> {     )
 
  [ 0 swap of 
    nested swap of ]        is  2array       (   n n --> [     )
 
 
  $ "Array width  (at least 2): " input $->n drop
  $ "Array length (at least 5): " input $->n drop
 
  say "Creating " over echo say " by " 
  dup echo say " array." cr
 
  2array
 
  say "Writing 12345 to element {1,4} of array." cr
 
  12345 swap ' [ 1 4 ] {poke}
 
  say "Reading element {1,4} of array: "
 
  ' [ 1 4 ] {peek} echo