Context
Let's have an input like '12,34,56,78,9,100'. The goal is to have an array of numbers [12], [34], ... in memory, reading comma-separated decimal numbers. To achieve it, we'll have to- Read a char
- If it's a comma (ASCII code 44), then start a new number
- Otherwise it's a digit, add it to ten times the current number
Note: this post is strongly related to array definition and read decimal number ones
Initial state
- Memory: 0, 0, 0, 0...
- Cursor: first cell
- Input: comma-separated decimal numbers (A,B,C,...)
Process
- Leave one cell to delimit array, plus one cell for current first integer read
- While a character is read
- Remove 44 (',' ASCII code)
- Set else bit
- If result is not null
- It's a digit. Remove 4 to get the decimal digit value
- Update current integer
- Otherwise
- Start a new integer
Code
>>
array delimiter and first integer (currently 0)
,[
>++++[-<----------->]+<
remove 44 (comma ASCII code) and set bit else
[>-<
if read value is not 44 => it is a digit
----
remove 4 again (to remove 48 which is 0's ASCII code)
>+++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>]
multiply current integer by ten and add new digit
]
>[-
else
>
move to next integer
]<
,]
Code (minified)
>>,[>++++[-<----------->]+<[>-<---->+++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>]]>[->]<,]
Final state
- Memory: 0,A,B,C,...,0
- Cursor: on final 0 (end of array)
- Input: empty
- Output: unchanged
Aucun commentaire:
Enregistrer un commentaire