digital literacy for everyone


[lit]

[generate-title]

[lit] [fig] #### arrlen prefixes length to each line of text #### license: creative commons cc0 1.0 (public domain) #### http://creativecommons.org/publicdomain/zero/1.0/ # x=$(echo hello | arrlen | awk '{print $1}') ; echo $x # 5 # $ for x in $(echo "hello there how are you?") ; do echo $x ; done | arrlen # 5 hello # 5 there # 3 how # 3 are # 4 you? # $ # create a function called delstr that deletes instances of delthis from string fromthis function delstr fromthis, delthis f = fromthis ; split f, delthis ; join f, "" ; return f fig # loop through whatever is piped (stdin) into arrlen... set each string to variable p forin p, stdin ## set cr to ascii 13, and set lf to ascii 10 cr = 13 ; chr lf = 10 ; chr z = p ; delstr z, cr ; delstr z, lf ## remove newlines from each string p = z ; len ; prints ; " " ; prints ## copy back to variable p, get length, put on screen p = z ; print ## copy again, put on screen next # stdin contains each line piped to arrlen. # forin copies each line of stdin to variable p, then runs other code until "next" is encountered. # then it goes to the next line of stdin and repeats the code in the "forin" loop. # except for block statements like forin and next, each line starts with a variable and each token can use that variable. # so this line: # p = z ; len ; prints ; " " ; prints # does the following: # * p = z (set p to contain the contents of variable z) # * len (change the string value of p to its own numeric length) # * prints (put the value of p on the screen, then stay on the same line) # * " " (change the value of p to one space, as a string) # * prints (put the value of p on the screen, stay on the line) # the difference between prints and print is that print goes to the next line afterwards
home: [lit]https://codeinfig.neocities.org[lit]