Simple explanation of some of those functions and an important alteration:
Names Default To Here( 1 );
//An associative array is a dictionary or hash map that links keys to values. Each key must be unique.
//Using a single column as the argument for the array gives us the unique values of the column with each value set to 1
//The <<Get Keys message then yields the keys only, so Associative Array( :Column ) << Get Keys is a way to get unique column values
//Jarmo's example of Summarize() does the same thing and is probably a better method, but the array method works on lists, too
Batch_vals = Associative Array( :Batch ) << Get Keys;
prefix = Left( :Batch[1], 3 );
suffix = Right( :Batch[1], 2 );
rootlength = Length( Batch_vals[1] ) - Length( prefix ) - Length( suffix );
//Transform Each performs the same action on each item in a list, matrix, or associative array and returns the transformed values in the same container
//I should have done this differently to avoid unintended substitutions, since a batch value that matched the prefix or suffix would be deleted.
//Batch_nums = Transform Each( {v, i}, Batch_vals, Num( Substitute( v, {prefix, suffix}, "" ) ) );
Batch_nums = Transform Each( {v, i}, Batch_vals, Num( Substr( v, Length( prefix ) + 1, rootlength ) ) );
Min_batch = Min( Batch_nums );
Max_batch = Max( Batch_nums );
Batches = Min_batch :: Max_batch;
dt = New Table( "Batches", New Column( "Batch Number", numeric, continuous, Set Values( Batches ) ) );
//Eval() always evaluates the code inside
//Eval Expr() evaluates all expressions inside before running the code
//When setting a column formula based on the value of a variable, I wrap that variable inside of Expr() and then use Eval( Eval Expr() )
//to pass the VALUE of the variable into the column formula, rather than passing the variable itself
//In this case, it wouldn't matter, since I delete the formula immediately after creation, but it's good practice
Eval(
Eval Expr(
dt << New Column( "Batch", character, Formula( Expr( prefix ) || Right( Char( :Batch Number ), rootlength, "0" ) || Expr( suffix ) ) )
)
);
dt:Batch << Delete Formula;