part 2: input commands in fig
some functionality is a matter of both input and output; in fig, functions can usually be separated into or at least categorized as one or the other.
the previous chapter mentioned arrshell, which can almost be categorized as an input command because it takes all the output of a command line shell and puts it into a variable. even if the shell session does other things, the purpose of arrshell is to collect and store information in the program itself and get it back into your fig program.
usually, the word “input” is used to describe commands that take information from a device connected to the computer– such as a drive, keyboard, or even another computer. there are some gray areas, because the computer treats some of the things it does with software as if those things were physical devices.
open is categorized simply as a function, because whether it opens a file on a drive for input or output depends on the value of a parameter. it acts as an input command when a file is opened for reading, and an output command when it opens for writing. you could refer to open as an “input/output” command, but in fig any command that isnt related to specifically to variables, input, output, math, loops or conditionals is simply lumped under “functions.”
now that weve distinguished between what really is and sort of is an input command, lets talk about the ones that are quite clearly related to input:
sets x to the number of seconds past midnight. it gets the value using the computers internal clock.
sets x to an array of all the information streamed into the program from whatever shell called it. this can be used to “chain programs together” including programs written in other languages or by other people. in unix-like operating systems and even dos and windows, this is called “piping” information from one program to another.
to pipe information from another program to a fig program, use arrstdin to get the information into an array.
to pipe information from a fig program into another program, simply use print on the fig side– the other program will use whatever accesses stdin on that end.
sets x to the string typed in on the keyboard. the program waits for the user to press “enter” after typing text in.
x "text.txt" open "r" y flineinput x
opens “text.txt” for input and reads a line using flineinput, which stores the line in variable y. if you had an incredibly large file and didnt want to open it all at once, this would be one way to avoid that. otherwise, arropen is easier to use instead:
x arropen "text.txt"
opens “text.txt” into array x. really, thats all you do. if you used open “r”, run “text.txt” close to close the file after youre done reading from it.
x time x date
while timer gets the seconds past midnight, time gets the hh:mm:ss time and date gets the mm/dd/yyyy date. unlike timer which is numeric, both time and date return strings.
x arrcurl "https://duckduckgo.com"
downloads the source of the page for https://duckduckgo.com and loads it into an array just like arropen would do for a local file.
x sleep 2
waits until 2 seconds have gone by to continue running the program. does not affect the main variable, and is only categorized as “input” because it gets information from the clock.
sets x to an array including each parameter the fig program was called with. didnt open the program you wrote with any parameters? then this command isnt going to do much for you.
if you are on the command line, this is how you call your program with parameters:
name-of-your-program.fig.py par1 par2 par3
the array will contain “par1”, “par2” and “par3”.
- license: creative commons cc0 1.0 (public domain)