Post Reply 
Free42: Random Word Picker
09-09-2022, 12:26 PM
Post: #1
Free42: Random Word Picker
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!
Visit this user's website Find all posts by this user
Quote this message in a reply
09-09-2022, 01:38 PM
Post: #2
RE: Free42: Random Word Picker
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
Visit this user's website Find all posts by this user
Quote this message in a reply
09-09-2022, 02:00 PM
Post: #3
RE: Free42: Random Word Picker
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.

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
09-09-2022, 04:10 PM
Post: #4
RE: Free42: Random Word Picker
Thanks guys! This works great, and I got the matrix in via copy-paste:
https://thomasokken.com/free42/#copy-paste

https://forum.swissmicros.com/viewtopic.php?f=15&t=3186

Regards,
David
Visit this user's website Find all posts by this user
Quote this message in a reply
09-09-2022, 07:33 PM
Post: #5
RE: Free42: Random Word Picker
(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.)
Visit this user's website Find all posts by this user
Quote this message in a reply
09-10-2022, 10:10 AM
Post: #6
RE: Free42: Random Word Picker
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
Visit this user's website Find all posts by this user
Quote this message in a reply
09-10-2022, 10:45 AM (This post was last modified: 09-10-2022 10:46 AM by rprosperi.)
Post: #7
RE: Free42: Random Word Picker
(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... Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
09-10-2022, 10:46 AM
Post: #8
RE: Free42: Random Word Picker
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
09-10-2022, 01:28 PM (This post was last modified: 09-11-2022 09:28 PM by Thomas Okken.)
Post: #9
RE: Free42: Random Word Picker
(09-10-2022 10:45 AM)rprosperi Wrote:  Well, not undocumented any more... Smile

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. Smile 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
Visit this user's website Find all posts by this user
Quote this message in a reply
09-11-2022, 12:34 PM (This post was last modified: 09-13-2022 08:16 AM by Thomas Okken.)
Post: #10
RE: Free42: Random Word Picker
Added documentation for generated-code utility functions:

https://thomasokken.com/plus42/#generated
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)