Swift Function reference
This section details functions that are available for use in the Swift language.
arg
Takes a command line parameter name as a string parameter and an optional default value and returns the value of that string parameter from the command line. If no default value is specified and the command line parameter is missing, an error is generated. If a default value is specified and the command line parameter is missing, @arg will return the default value.
Command line parameters recognized by @arg begin with exactly one hyphen and need to be positioned after the script name.
For example:
trace(arg("myparam"));
trace(arg("optionalparam", "defaultvalue"));
$ swift arg.swift -myparam=hello
Swift v0.3-dev r1674 (modified locally)
RunID: 20080220-1548-ylc4pmda
Swift trace: defaultvalue
Swift trace: hello
extractInt
extractInt(file) will read the specified file, parse an integer from the file contents and return that integer.
extractFloat
Similar to extractInt, extractFloat(file) will read the specified file, parse a float from the file contents and return that float.
filename
filename(v) will return a string containing the filename(s) for the file(s) mapped to the variable v. When more than one filename is returned, the filenames will be space separated inside a single string return value.
filenames
filenames(v) will return multiple values containing the filename(s) for the file(s) mapped to the variable v.
length
length(array) will return the length of an array in Swift. This function will wait for all elements in the array to be written before returning the length.
readData
readData will read data from a specified file and assign it to Swift variable. The format of the input file is controlled by the type of the return value. For scalar return types, such as int, the specified file should contain a single value of that type. For arrays of scalars, the specified file should contain one value per line. For complex types of scalars, the file should contain two rows. The first row should be structure member names separated by whitespace. The second row should be the corresponding values for each structure member, separated by whitespace, in the same order as the header row. For arrays of structs, the file should contain a heading row listing structure member names separated by whitespace. There should be one row for each element of the array, with structure member elements listed in the same order as the header row and separated by whitespace. The following example shows how readData() can be used to populate an array of Swift struct-like complex type:
type Employee{
string name;
int id;
string loc;
}
Employee emps[] = readData("emps.txt");
Where the contents of the "emps.txt" file are:
name id loc
Thomas 2222 Chicago
Gina 3333 Boston
Anne 4444 Houston
This will result in the array "emps" with 3 members. This can be processed within a Swift script using the foreach construct as follows:
foreach emp in emps{
tracef("Employee %s lives in %s and has id %d", emp.name, emp.loc, emp.id);
}