How to resolve the algorithm Optional parameters step by step in the UNIX Shell programming language
How to resolve the algorithm Optional parameters step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
Define a function/method/subroutine which sorts a sequence ("table") of sequences ("rows") of strings ("cells"), by one of the strings. Besides the input to be sorted, it shall have the following optional parameters: This task should be considered to include both positional and named optional parameters, as well as overloading on argument count as in Java or selector name as in Smalltalk, or, in the extreme, using different function names. Provide these variations of sorting in whatever way is most natural to your language. If the language supports both methods naturally, you are encouraged to describe both. Do not implement a sorting algorithm; this task is about the interface. If you can't use a built-in sort routine, just omit the implementation (with a comment). See also:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Optional parameters step by step in the UNIX Shell programming language
Source code in the unix programming language
#!/usr/bin/env bash
# sort-args.sh
data() {
cat <
123 456 0789
456 0789 123
0789 123 456
EOF
}
# sort_table [column NUM | KIND | reverse] ... OUTPUT
# KIND = lexicographical | numeric | human
sort_table() {
local opts='-b'
local column=1
while (( $# > 0 )) ; do
case "$1" in
column|col|c) column=${2?Missing column number} ; shift ;;
lexicographical|lex|l) opts+=' -d' ;;
numeric|num|n) opts+=' -g' ;;
human|hum|h) opts+=' -h' ;;
reverse|rev|r) opts+=' -r' ;;
esac
shift
done
eval "sort $opts -k $column,$column -"
}
echo sort defaults ; data | sort_table
echo sort defaults reverse ; data | sort_table reverse
echo sort column 2 ; data | sort_table col 2
echo sort column 2 reverse ; data | sort_table col 2 reverse
echo sort numeric ; data | sort_table numeric
echo sort numeric reverse ; data | sort_table numeric reverse
You may also check:How to resolve the algorithm Van Eck sequence step by step in the 11l programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Ursa programming language
You may also check:How to resolve the algorithm Minesweeper game step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the GML programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Go programming language