F.E.L.T.

Functionally Equivalent Language Translation

AREF / @

(aref var-name 0)    ; read 0th element of var-name
(aref bar :names i)  ; read 'i'th element of bar / names
(aref baz i j k)     ; read the i-th / j-th / k-th element
(@ var-name 0)       ; read 0th element of var-name
(@ bar :names i)     ; read 'i'th element of bar / names
(@ baz i j k)        ; read the i-th / j-th / k-th element

The AREF instruction lets you generate an offset into an array for reading or writing if used as the left-hand side of the =(ASSIGN) instruction. The first token after the @ is the array variable-name. The second and subsequent tokens are constructed into an access path according to the syntax rules of the back-end language.


The AREF instruction or its short-form @ is how you access an array type variable in a FELT source text. It would be good to remember now that @ can be read as 'array' and that will help you to remember the other short-forms for the various push, pop, shift and unshift instructions as listed under the see also section.

Array Short-form names

It's worth stating them here so that you can familiarise yourself with them as they save a lot of typing and make for more concise code, and the code is certainly no more or less readable then 'perl' code. I am not saying if that's good or bad.

As stated, if you read the word 'array' when you see the @ symbol, then the position of the > and < symbols should make sense.

@<
This sequence, @<, can then be read as 'push to the end of the array'. It is the same as the APUSH instruction. I call this one 'quack' as well. ;)
@>
This sequence, @>, can then be read as 'pop from the end of the array'. It is the same as the APOP instruction.
<@
This sequence, <@, can then be read as 'pop from the front of the array'. It is the same as the ASHIFT instruction.
>@
This sequence, >@, can then be read as 'push to the front of the array'. It is the same as the AUNSHIFT instruction.

Read Examples

Some examples of using the AREF instruction follow, first as read operations and then as write operations, first the FELT source:

(defvar data [ [1 2 3] {:five "Five!"}])
(defvar x (+ (aref data 0 1) (aref data 0 2)))
(defvar y (concat "The Famous " (@ data 1 :five)))

The above declarations produce this when translated to PHP:

$data = array(array(1, 2, 3), array('five' => "Five!"));
$x = ($data[0][1] + $data[0][2]);
$y = ("The Famous " . $data[1]['five']);

And this when rendered as javaScript:

var data = [[1, 2, 3], {'five' : "Five!"}];
var x = (data[0][1] + data[0][2]);
var y = ("The Famous " + data[1]['five']);

Adding some console output code and then running the javaScript through 'node' gives the following output:

[ [ 1, 2, 3 ], { five: 'Five!' } ]
5
The Famous Five!

The actual code that produce the above output was:

(defvar data [ [1 2 3] {:five "Five!"}])
(defvar x (+ (aref data 0 1) (aref data 0 2)))
(defvar y (concat "The Famous " (@ data 1 :five)))

(emit data)
(emit x)
(emit y)

Write Examples

The way to modify the contents of an array, be it a normal or association type, are exactly the same in FELT. The back-end coder will generate the appropriate code at translation time.

It helps if you have understood the =(ASSIGN) instruction as that is how you modify an array slot.

Here are some examples and the equivalent PHP code:

(defvar data [ [1 2 3] {:five "Five!"}])
(defvar x (+ (aref data 0 1) (aref data 0 2)))
(defvar y (concat "The Famous " (@ data 1 :five)))

(setq (aref data 0 0) 100)
(setq (aref data 0 1) 200)
(setq (aref data 0 2) 300)
(= (@ data 1 :five) "Cinq!")

When run through the PHP coder we get this output:

$data = array(array(1, 2, 3), array('five' => "Five!"));
$x = ($data[0][1] + $data[0][2]);
$y = ("The Famous " . $data[1]['five']);
$data[0][0] = 100;
$data[0][1] = 200;
$data[0][2] = 300;
$data[1]['five'] = "Cinq!";

And the javaScript code:

var data = [[1, 2, 3], {'five' : "Five!"}];
var x = (data[0][1] + data[0][2]);
var y = ("The Famous " + data[1]['five']);
data[0][0] = 100;
data[0][1] = 200;
data[0][2] = 300;
data[1]['five'] = "Cinq!";