38 — Name list: reading a file line by line
Guide 37 saved one piece of text. When there are several names, it is easier to keep one per line and turn them back into a list on the way in.
A program that uses files does not run on the site. Try this on your own computer.
Steps
Join the list into one piece of text. A newline between them puts one on each line:
run it →set friends to list of Mina, Ada, Grace set text to friends joined by newline show textSave that text to a file:
run it →set friends to list of Mina, Ada, Grace set text to friends joined by newline write text to "names.txt"Read it back and turn it into a list again.
split by linedoes that:
run it →read "names.txt" into memo set names to memo split by line show how many namesYou can walk it knowing which one you are on:
run it →set names to list of Mina, Ada for each name in names with place show place show name endplacecounts from 1.All of it:
run it →set friends to list of Mina, Ada, Grace set text to friends joined by newline write text to "names.txt" read "names.txt" into memo set names to memo split by line show how many names for each name in names with place show place show name endjoinedandsplitare opposites. A list becomes text and the text becomes a list again, with a file in between.
Try it yourself
Ask for names, put each in a list, and save the file when you are done. Next time, read that file first and add to it, so the list grows.
What you learned
<list> joined by newlinemakes text with one item per line.<text> split by lineturns it back into a list.- A file between the two makes the list outlive the program.
with placegives you which one you are on, counting from 1.