Seed Native Functions
This was encountered in the Hello World example. The purpose of this to output the string supplied with this command to the console. The format of it as follows:
print ("This is a test string");
It can only take one string at a time and if any other are given, it comes up with the following error message.
ArgumentError print expected 1 argument, got 2
If you wanted to display more than one string on, than you would join them using the + operator. An example of this is as follows:
print ("Hello" + " " + "world");
As mentioned, print displays the string supplied with it. If you supply another type of variable like an integer, it will automatically convert it to a string and display it. This can be seen as:
print (42); print (true); print (42.33);
For info, print and Seed.print are the same functions.
To use command line arguments, use the Seed.argv command. This contains an array of the command line arguments used when running the program. In this array, it will include the seed command, the program name and command line arguments. The first argument will always be stored in Seed.argv.
An example program to use command line arguments is as follows:
To see if command line arguments have entered, use the following if statement:
if (Seed.argv.length > 2)
and to display the argument:
print (Seed.argv + " says Hello, World!");
If more than one argument was entered, it would be at index Seed.argv and so forth. To display all the arguments given in a line, use the following code:
The for statement will be explained later.
Seed.sprintf is similar to printf (or Seed.printf) but is more versatile as it allows you to display variables with the string you want to display on console. This is done by putting format specifiers in the string. The basic format specifiers are as follows:
Float variable or decimal number i.e. 3.3
An example of this is as follows:
Seed.sprintf("hello %s, your age is %d", "John", 25);
The output of this is as follows:
hello John, your age is 25
This can also be done with variables:
var user = "Robert Bruce"; var access_level = 42; Seed.sprintf("user %s has an access level of %d", user, access_level);
The output from this is:
user Robert Bruce has an access level of 42
Another facility of sprintf is that you can format the output of the data.
In this example, you can specify the number of decimal points that can be displayed when displaying a float variable:
Seed.sprintf ("%.2f", 3.1543);
The output is:
Contrast this with the ordinary printf statement:
You can also right align text by padding it with spaces for better visual output. This means that if you used multiple format specifiers in the string, you can have them the same width. An example of this would be if you had string that is 4 characters long you can specify it output it with a width of 6 which would add two spaces in front of this. To do this, insert a number in the format specifier which is the size of the width you want the output to be.
In the below example, the width of output is 10.
this is done by the following:
Which gives ouput:
Another example which gives a width of 3 for each string outputted:
Seed.sprintf("%3s %3s %3s\n%3s %3s %3s", "a", "hhh", "gg", "xxx", "kk", "b");
As you can see this makes the output better aligned:
a hhh gg xxx kk b
Numerical input can also be aligned by padding them with zeros Again this is the same as width which rather display spaces before, displays zeros instead.
In this example, the number is displayed as a 5 digit number:
The following displays the following as 3 digit numbers:
Seed.sprintf("%03d\n%03d\n%03d", 223, 2, 38);
The output being
223 002 038
There may be times when your program may require to run or spawn a Linux shell or external program installed on your system. This is done by the Seed.Spawn command. The format of this is as follows:
variable = Seed.spawn(command);
For example the following command plays the mp3 file temp.mp3 using the mplayer program.
var spawn_output = Seed.spawn("mplayer temp.mp3");
The variable spawn_output is required as the Seed.spawn can return information about the command it spawned. This is useful if the command outputs text to the terminal or outputs an error message.
To get output use the following command:
If the program spawned had an error then the following would be used to check for it.
print(variable.stderr); The following example uses the bash command to get the name of current user logged into Linux and stores it into string variable logged_in_user:
var spawn_output = Seed.spawn("id -un");
var logged_in_user = spawn_output.stdout;
The following example uses the bash command to get the name of current user logged into Linux and stores it into string variable logged_in_user: