Arrays
This topic is meant to document arrays in Pile
Overview
In Pile, Array is a compound structure containing any possible type and with any length.
Arrays can be used to store ordered information in one go, threated as one single value on the data stack.
Creating Arrays
To create arrays in Pile, use the array
keyword and close it with end
.
Syntax:
array
<CODE>
end
See that the array construction accepts code inside its block. The way arrays work is by adding all the elements that are pushed on the stack during the execution of the block into the array being created. After that, the array element is pushed on the stack.
Here are some examples on how to experiment with arrays in Pile:
# All the values pushed on the stack inside the array..end block are inserted into the array in order
array
3 4 + # This results in 7
6 # This results in 6
# Final stack result: [ 7 6 ]
end
# Now the stack contains only one array, the items added to the stack
# inside the block were inserted into the array itself
This approach to array creation enables a big variety of ways to generate arrays as you want. For example, you can use loops to create a range:
# All the values pushed on the stack inside the array..end block are inserted into the array in order
array
# Generates a range from 0 to 10
10 0 loop dup rot over over = if break end
rot
1 +
end
drop drop
# Keeps pushing the counter on the stack, it will be later added to the array
end