Given a string representing a matrix of numbers, return the rows and columns of that matrix.
So given a string with embedded newlines like:
9 8 7
5 3 2
6 6 7
representing this matrix:
1 2 3
|---------
1 | 9 8 7
2 | 5 3 2
3 | 6 6 7
your code should be able to spit out:
The rows for our example matrix:
And its columns:
This is the first exercise where the solution is not a "main" script. The goal is to write a library of functions to be included into other awk programs. There are several interesting new concepts to see in this exercise.
The following sections that are tagged with "(gawk)" are specifically GNU awk extensions.
In this exercise, you'll be reading data from a file, not from the main input stream.
Read about getline
in the Gnu awk manual,
particularly the Getline/File
and Getline/Variable/File
forms.
A two-dimensional array might be a way to implement this solution. GNU awk has two ways to represent one:
Notice the @include
directive in the test file.
This instructs gawk to read and evaluate the named file.
All awk variables are global (except for function parameters which are local to the function).
There is a high potential for name collisions, particularly when third-party libraries get included.
Namespaces are a way to partition where variables are stored.
Notice the @namespace
directive in the exercise's files.
The default namespace is named "awk". Having a default namespace allows the programmer to call a builtin awk function from inside a function in a different namespace.
Array parameters are passed by reference. Changes to the array made in the function are visible in the caller.
Non-array parameters are passed by value.
For untyped parameters, it depends on what the function does with them:
Full details are in the manual in Passing Function Arguments by Value Or by Reference.
Function scoped (local) variables can be created by using "pass by value" parameters. It is not an error to pass fewer values to a function than the number of listed parameters; the excess parameters are "untyped" until they get used. They are available to be assigned scalar values in the function that are not stored in the global namespace.
By convention, in the function signature the expected parameters appear first followed by some whitespace and then the local parameters. An example:
function add(a, b, total) {
# here, `typeof(total)` is "untyped"
total = a + b
# now, `typeof(total)` is "number"
return total
}
BEGIN {
sum = add(5, 10)
print sum # 15
print typeof(total) # untyped, meaning "total" is unused in this scope
}
Sign up to Exercism to learn and master AWK with 79 exercises, and real human mentoring, all for free.