aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions/array-stuff
diff options
context:
space:
mode:
Diffstat (limited to 'examples/functions/array-stuff')
-rw-r--r--examples/functions/array-stuff103
1 files changed, 103 insertions, 0 deletions
diff --git a/examples/functions/array-stuff b/examples/functions/array-stuff
new file mode 100644
index 0000000..97ed512
--- /dev/null
+++ b/examples/functions/array-stuff
@@ -0,0 +1,103 @@
+# usage: reverse arrayname
+reverse()
+{
+ local -a R
+ local -i i
+ local rlen temp
+
+ # make r a copy of the array whose name is passed as an arg
+ eval R=\( \"\$\{$1\[@\]\}\" \)
+
+ # reverse R
+ rlen=${#R[@]}
+
+ for ((i=0; i < rlen/2; i++ ))
+ do
+ temp=${R[i]}
+ R[i]=${R[rlen-i-1]}
+ R[rlen-i-1]=$temp
+ done
+
+ # and assign R back to array whose name is passed as an arg
+ eval $1=\( \"\$\{R\[@\]\}\" \)
+}
+
+A=(1 2 3 4 5 6 7)
+echo "${A[@]}"
+reverse A
+echo "${A[@]}"
+reverse A
+echo "${A[@]}"
+
+# unset last element of A
+alen=${#A[@]}
+unset A[$alen-1]
+echo "${A[@]}"
+
+# ashift -- like shift, but for arrays
+
+ashift()
+{
+ local -a R
+ local n
+
+ case $# in
+ 1) n=1 ;;
+ 2) n=$2 ;;
+ *) echo "$FUNCNAME: usage: $FUNCNAME array [count]" >&2
+ exit 2;;
+ esac
+
+ # make r a copy of the array whose name is passed as an arg
+ eval R=\( \"\$\{$1\[@\]\}\" \)
+
+ # shift R
+ R=( "${R[@]:$n}" )
+
+ # and assign R back to array whose name is passed as an arg
+ eval $1=\( \"\$\{R\[@\]\}\" \)
+}
+
+ashift A 2
+echo "${A[@]}"
+
+ashift A
+echo "${A[@]}"
+
+ashift A 7
+echo "${A[@]}"
+
+# Sort the members of the array whose name is passed as the first non-option
+# arg. If -u is the first arg, remove duplicate array members.
+array_sort()
+{
+ local -a R
+ local u
+
+ case "$1" in
+ -u) u=-u ; shift ;;
+ esac
+
+ if [ $# -eq 0 ]; then
+ echo "array_sort: argument expected" >&2
+ return 1
+ fi
+
+ # make r a copy of the array whose name is passed as an arg
+ eval R=\( \"\$\{$1\[@\]\}\" \)
+
+ # sort R
+ R=( $( printf "%s\n" "${A[@]}" | sort $u) )
+
+ # and assign R back to array whose name is passed as an arg
+ eval $1=\( \"\$\{R\[@\]\}\" \)
+ return 0
+}
+
+A=(3 1 4 1 5 9 2 6 5 3 2)
+array_sort A
+echo "${A[@]}"
+
+A=(3 1 4 1 5 9 2 6 5 3 2)
+array_sort -u A
+echo "${A[@]}"