From f76d232e73a669f27ca1100227c52d727c6aef35 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 07:55:13 +1000 Subject: [PATCH] docs: update --- README.md | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3af398c..5735aaa 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ scripts and not full blown utilities. * [Strip characters from end of string.](#strip-characters-from-end-of-string) * [Arrays](#arrays) * [Reverse an array.](#reverse-an-array) + * [Cycle through an array.](#cycle-through-an-array) + * [Toggle between two values.](#toggle-between-two-values) * [File handling](#file-handling) * [Read a file to a string.](#read-a-file-to-a-string) * [Read a file to an array (*by line*).](#read-a-file-to-an-array-by-line) @@ -49,7 +51,6 @@ scripts and not full blown utilities. * [Arithmetic](#arithmetic) * [Simpler syntax to set variables.](#simpler-syntax-to-set-variables) * [Ternary tests.](#ternary-tests) - * [Cycle through an array.](#cycle-through-an-array) * [Colors](#colors) * [Convert a hex color to RGB.](#convert-a-hex-color-to-rgb) * [Convert an RGB color to hex.](#convert-an-rgb-color-to-hex) @@ -160,6 +161,28 @@ reverse_array() { } ``` +### Cycle through an array. + +Each time the `printf` is called, the next array element is printed. When +the print hits the last array element it starts from the first element +again. + +```sh +arr=(a b c d) +printf '%s\n' "${arr[$((i==${#arr[@]}-1?i=0:++i))]}" +``` + + +### Toggle between two values. + +This works the same as above, this is just a different use case. + +```sh +arr=(true false) +printf '%s\n' "${arr[$((i==${#arr[@]}-1?i=0:++i))]}" +``` + + ## File handling @@ -328,22 +351,6 @@ basename() { ((var=var2>var?var2:var)) ``` -### Cycle through an array. - -Each time the `printf` is called, the next array element is printed. When -the print hits the last array element it starts fromt he first element -again. - -```sh -# Cycle through multiple elements. -arr=(a b c d) -printf '%s\n' "${arr[$((i==${#arr[@]}-1?i=0:++i))]}" - -# Cycle through two elements (works like a toggle). -arr=(true false) -printf '%s\n' "${arr[$((i==${#arr[@]}-1?i=0:++i))]}" -``` - ## Colors