Added random array element
This commit is contained in:
25
README.md
25
README.md
@@ -65,6 +65,7 @@ Any donations are appreciated, they give me the time to make this the best resou
|
|||||||
* [Arrays](#arrays)
|
* [Arrays](#arrays)
|
||||||
* [Reverse an array.](#reverse-an-array)
|
* [Reverse an array.](#reverse-an-array)
|
||||||
* [Remove duplicate array elements.](#remove-duplicate-array-elements)
|
* [Remove duplicate array elements.](#remove-duplicate-array-elements)
|
||||||
|
* [Random array element.](#random-array-element)
|
||||||
* [Cycle through an array.](#cycle-through-an-array)
|
* [Cycle through an array.](#cycle-through-an-array)
|
||||||
* [Toggle between two values.](#toggle-between-two-values)
|
* [Toggle between two values.](#toggle-between-two-values)
|
||||||
* [Loops](#loops)
|
* [Loops](#loops)
|
||||||
@@ -570,6 +571,30 @@ green
|
|||||||
blue
|
blue
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Random array element.
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
random_array_element() {
|
||||||
|
# Usage: random_array_element "array"
|
||||||
|
arr=("$@")
|
||||||
|
printf '%s\n' "${arr[RANDOM % $#]}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ array=(red green blue yellow brown)
|
||||||
|
$ random_array_element "${array[@]}"
|
||||||
|
yellow
|
||||||
|
|
||||||
|
# You can also just pass multiple arguments.
|
||||||
|
$ random_array_element 1 2 3 4 5 6 7
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
## Cycle through an array.
|
## Cycle through an array.
|
||||||
|
|
||||||
Each time the `printf` is called, the next array element is printed. When
|
Each time the `printf` is called, the next array element is printed. When
|
||||||
|
|||||||
Reference in New Issue
Block a user