retroforth/example/parse-ups.retro
crc 9e03717deb normalize names for examples (with a couple of exceptions), closes #38
FossilOrigin-Name: 088675e452ed86a712563c8b2597fe4d47da59bdea0e40becdd1e028a84c47b0
2021-01-24 01:13:04 +00:00

20 lines
662 B
Forth

This code parses a UPS Type 1Z tracking number and returns the
subsections in a readable format.
See http://osiris.978.org/~alex/ups.html for a list of service codes.
~~~
:valid? [ #0 #2 s:substr '1Z s:eq? ] sip swap ;
:account [ #2 #6 s:substr 'Account:_ swap s:append ] sip ;
:service-code [ #8 #2 s:substr 'Service_code:_ swap s:append ] sip ;
:package-id [ #10 #7 s:substr 'Package_ID:_ swap s:append ] sip ;
:checksum #17 + 'Checksum:_ swap s:append ;
:parse-ups
valid? [ account service-code package-id checksum
#4 [ s:put nl ] times ]
[ drop 'invalid_tracking_number s:put nl ] choose ;
~~~
```
'1Z6x36270342495730 parse-ups
```