I was trying to pick a random row in a table that adheres to certain conditions. In other words, possibly use a table as a database.
So I was wondering if I was missing a shortcut. I was able to do what I wanted, but the code doesn’t seem elegant. The code below works–basically, I want to choose a random row with bb=1.
I have some simpler syntactical tries below, and they seem like they should work, but they all failed me. What am I missing, if anything? I’m quite possibly a bit spoiled by the natural language Inform does understand, here…
[code]“randomrow” by Andrew Schultz
table of randrow
aa bb cc
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
room 1 is a room.
when play begins:
let LL be a list of numbers;
repeat with Q running from 1 to number of rows in table of randrow:
choose row Q in table of randrow;
if bb entry is 1:
add Q to LL;
let T2 be number of entries in LL;
let Q2 be a random number from 1 to T2;
let Q3 be entry Q2 of LL;
choose row Q3 in table of randrow;
say “[Q3] [aa entry] [bb entry] [cc entry].”;
[definition: a row (called wor) is super-awesome:
if bb entry is 1:
decide yes;
decide no;
when play begins: [neither of these work at least as coded]
choose a random row with bb of 1 in table of randrow;
choose a random super-awesome row in the table of randrow;
say “[aa entry] [bb entry] [cc entry].”;][/code]
Thanks!