2017 Problem Authoring Workshop

Variable-length Tables?

Variable-length Tables?

by Barbra Steinhurst -
Number of replies: 2
Okay, so this feels really advanced and complicated, if it is even possible...

I'd like to build a T-table with the first column x-coordinates and the second column y-coordinates. I'd like the first row to be a randomly-generated pair (x1,y1) and the last row to be a randomly-generated pair (x2,y2). I'd like the in-between rows to be x1+1, x1+2, x1+3, etc up to x2. But the number of these rows, as well as the contents therein, vary depending on what x1 and x2 are. I may be totally dreaming that it could be this customizable. But now I've got the curiosity bug to find out.
In reply to Barbra Steinhurst

Re: Variable-length Tables?

by Barbra Steinhurst -
Hm... is it possible to define an array that starts with variable x1 then counts up by 1's to variable x2? Then use the length_array variable to define the number of rows in the table?
In reply to Barbra Steinhurst

Re: Variable-length Tables?

by Davide Cervone -
Here is an example that creates a table with rows for each x between two randomly selected values. Perhaps that will be sufficient for you to modify it for your needs:
loadMacros(
  "MathObjects.pl",
  "PGML.pl",
  "niceTables.pl"
);

$x1 = random(-5,5,1);
$x2 = random($x1+1,$x1+5,1);

$f = Formula("x/2+1");

#
#  The first row of the table is headers for the columns
#    (formatted using LaTeX)
#
@xy = ([['\(x\)', header=>'CH'], ['\(y\)', header=>'CH']]);

#
#  Add the rows to the table.  The values are put between
#     \(  and \) so they will be formatted as mathematics
#
for $x ($x1..$x2) {
  push(@xy, ['\('.$x.'\)', '\('.$f->eval(x => $x).'\)']);
}

#
#  Create the table using DataTable().  The three stars are
#     so that the LaTeX will be formatted after being inserted.
#
BEGIN_PGML
    [@ DataTable( [ @xy ], align => "|c|c|", midrules=>1) @]***
END_PGML
Hope that gets you where you need to go.