I’d like to write a program which randomly, or I suppose pseudorandomly, selects one word each from two large word lists, and hyphenates them, printing the result.
As an example, given these word lists:
1. fig, frog, cardboard, gelatin
2. soft, angry, instant, canned
The program, when run, might output the following:
canned-fig
canned-cardboard
instant-frog
The question:
How do I store the word lists? Is there such a thing as calling a file in free42? I figure someone has a more optimal way than simple listing them all in the code!
Thanks!
There are no files, but matrices will do the job nicely, considering the six-character string length limit of the HP-42S no longer applies in Free42.
You can just put each word list in a text file, one word per line. Copy the text and paste it into Free42, and presto, you have a Nx1 matrix, with one row for each line in the text.
With a list stored under the name LIST, you can select a random element from it as follows:
01 INDEX "LIST"
02 RCL "LIST"
03 DIM?
04 ×
05 RAN
06 ×
07 IP
08 1
09 +
10 LASTX
11 STOIJ
12 RCLEL
alternatively:
01 INDEX "LIST"
02 I-
03 RCLIJ
04 x
03 RAN
06 x
07 1
08 STO+ ST Y
09 STOIJ
10 RCLEL
or, if you have the size of LIST in "N":
01 INDEX "LIST"
02 RAN
03 RCLx "N"
04 1
05 STO+ ST Y
06 STOIJ
07 RCLEL
Cheers, Werner
I originally tried it with lists, but getting the N-th item out of a list is a nightmare, or I'm missing something.
(09-09-2022 02:00 PM)Werner Wrote: [ -> ]I originally tried it with lists, but getting the N-th item out of a list is a nightmare, or I'm missing something.
Well, you could cobble something together using LIST→ and PICK, but it wouldn't be pretty and it would require NSTK mode. And in Plus42, you could use GETITEM, but not in Free42.
(GETITEM is an undocumented function which is used to implement matrix and list element access in code generated by PARSE, but there's nothing magical about those functions used by the code generator, you can use them in your own code as well.)
Here's one way to extract a random element from a list:
00 { 23-Byte Prgm }
01▸LBL "RANITEM"
02 ENTER
03 LENGTH
04 RAN
05 ×
06 SUBSTR
07 HEAD ST X
08 END
(09-09-2022 07:33 PM)Thomas Okken Wrote: [ -> ][snip]
(GETITEM is an undocumented function which is used to implement matrix and list element access in code generated by PARSE, but there's nothing magical about those functions used by the code generator, you can use them in your own code as well.)
Well, not undocumented any more...

More generally, to make lists a bit easier to work with in programs, I guess it would be better to extend GETITEM and PUTITEM, so the first parameter doesn't have to be a name but can also be a matrix or list, and then back-port them to Free42.
(09-10-2022 10:45 AM)rprosperi Wrote: [ -> ]Well, not undocumented any more... 
Technically, all I have done so far, in this and one other thread and on my web site, is to mention the existence of GETITEM.

An actual description of how it works, and also all the other functions from the Generated Code section, is still missing...
Quote:Generated Code:
GTOL XEQL GSTO GRCL SVAR GETITEM PUTITEM = ≠ < > ≤ ≥ && || ^^ ! IF? TRUNC DDAYSC GETEQN →PAR FDEPTH RAISE