en:pfw:day-of-the-week
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
en:pfw:day-of-the-week [2023-09-04 18:12] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:day-of-the-week [2023-09-04 18:12] (current) – ↷ Seite von pfw:day-of-the-week nach en:pfw:day-of-the-week verschoben uho | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{pfw: | ||
+ | ====== Day of The Week ====== | ||
+ | |||
+ | In this programming pearl Albert Nijhof shows how to extract strings form a sequence of strings. | ||
+ | |||
+ | The idea here is to represent the sequence of strings as a single string. | ||
+ | |||
+ | <code forth> | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 10 s" SunMonTueWedThuFriSat???" | ||
+ | 11 swap 3 * + 3 type space ; | ||
+ | 12 | ||
+ | 13 \ 3 .day -> wed | ||
+ | 14 \ 8 .day -> ??? | ||
+ | 15 | ||
+ | 16 \ ===== For variable string length ===== | ||
+ | 17 : .DAY ( n -- ) \ n in [0,6] | ||
+ | 18 7 umin | ||
+ | 19 s" 3Sun 3Mon 4Tues 6Wednes 5Thurs 3Fri 5Satur 1?" drop | ||
+ | 20 swap 0 | ||
+ | 21 ?do begin count bl = until | ||
+ | 22 loop count [char] 0 - type | ||
+ | 23 ." day " ; | ||
+ | 24 | ||
+ | 25 \ 3 .day -> Wednesday | ||
+ | 26 \ 9 .day -> ?day | ||
+ | 27 | ||
+ | 28 \ ===== For longer strings ===== ( :septiembre ) | ||
+ | 29 : M, ( adr len -- ) 0 ?do count c, loop drop ; | ||
+ | 30 decimal create (MESES) | ||
+ | 31 char " parse 5enero 7febrero 5marzo 5abril 4mayo 5junio 5julio " | ||
+ | 32 char " parse 6agosto :septiembre 7octubre 9noviembre 9diciembre 1?" m, | ||
+ | 33 align | ||
+ | 34 : .MES ( n -- ) \ n in [1,12] | ||
+ | 35 1- 12 umin (meses) swap 0 | ||
+ | 36 ?do begin count bl = until | ||
+ | 37 loop count [char] 0 - type space ; | ||
+ | 38 | ||
+ | 39 \ 9 .mes -> septiembre | ||
+ | 40 \ 0 .mes -> ? | ||
+ | 41 | ||
+ | 42 \ <><> | ||
+ | </ | ||
+ | |||
+ | ===== Extract constant-length strings ===== | ||
+ | |||
+ | First let's assume all element strings of the sequence have the same length. | ||
+ | |||
+ | Then the start address of the string n in the sequence can be calculated by | ||
+ | |||
+ | < | ||
+ | «address of string n» = «start-address of sequence» + n * «fixed length of element strings» | ||
+ | </ | ||
+ | |||
+ | In Forth that looks like the definition of '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | ===== Extract strings of variable length ===== | ||
+ | |||
+ | If the element strings in the sequence are not all of the same length, then their individual lengths need to be stored as well. The lookup then has to traverse the sequence on a string by string basis to find the nth string. | ||
+ | |||
+ | The definition of '' | ||
+ | The range check in line 18 is as described above.\\ | ||
+ | The represented sequence (line 19) now has a length byte in front of each element string. Character ' | ||
+ | |||
+ | Line 21 ?terates over the sequence to the nth element string. It assumes that not only is their individual length embedded but also that they are separated by a single space character: The '' | ||
+ | The surrounding '' | ||
+ | |||
+ | If n is out of range 0 to 6 then '' | ||
+ | n is mapped to 7 (line 18), the last element string is found (" | ||
+ | |||
+ | So '' | ||
+ | |||
+ | ===== Long sequence ===== | ||
+ | |||
+ | In the english version displaying days of the week the encoded sequence (line 19) has a length of 45 characters. Short enough to be represented in a single line of source code.\\ | ||
+ | If however the string to be stored in memory is larger than a single line, it might be better to construct it in a different way. | ||
+ | |||
+ | Line 30-33 show how to do it. They assume that a word '' | ||
+ | |||
+ | Line 30 gives the name '' | ||
+ | Line 31 and 32 use '' | ||
+ | |||
+ | The definition of '' | ||
+ | |||
+ | ===== Summary ===== | ||
+ | |||
+ | It is possible to store sequences of strings as single strings in memory and it is easy to extract the individual element strings:\\ | ||
+ | If all element strings have fixed length you can do address calculation, | ||
+ | |||
+ | Lengths can be encoded as characters. | ||
+ | |||
+ | Longer strings can be constructed step by step in memory. | ||
+ | |||
+ | |||
+ | ===== Contributions ===== | ||
+ | |||
+ | < | ||