Skip to content

Latest commit

 

History

History
194 lines (114 loc) · 4.94 KB

array.md

File metadata and controls

194 lines (114 loc) · 4.94 KB

The array module

The array module implements functions for sorting, searching, printing, and comparing arrays.

Dependencies

The array module does not depend on other modules.

Function index

Function Purpose
array_contains() Check if an element is in an array
array_identical() Check if two arrays are identical
array_same() Check if two arrays contain the same elements
array_sort() Sort an array
array_to_lines() Print an array, one element per line

array_contains()

Check whether an element is contained in an array.

Synopsis

array_contains "$element" "${array[@]}"

Description

The array_contains() function checks whether element is contained in array. It does so by iterating over the array, comparing each element in the array with element.

Return value

Return value Meaning
0 The element is contained in the array
1 The element was not found in the array

Standard input

array_contains() does not read from standard input.

Standard output

array_contains() does not write to standard output.

Standard error

array_contains() does not write to standard error.

array_identical()

Checks if two arrays are identical.

Synopsis

array_identical left right

Description

The array_identical() function checks whether the elements in the arrays referenced by left and right are identical. That means, it checks if the two arrays contain the same elements in the same order. The parameters left and right are name references to the arrays to be compared.

Return value

Return value Meaning
0 The arrays are identical
1 The arrays are not identical

Standard input

array_identical() does not read from standard input.

Standard output

array_identical() does not write to standard output.

Standard error

array_identical() does not write to standard error.

array_same()

Check if two arrays contain the same elements.

Synopsis

array_same left right

Description

The array_same() function checks whether the arrays referenced by left and right contain the same elements. That means, it checks if the arrays are permutations. The parameters left and right are name references to the arrays to be compared.

Return value

Return value Meaning
0 The arrays contain the same elements
1 The arrays do not contain the same elements

Standard input

array_same() does not read from standard input.

Standard output

array_same() does not write to standard output.

Standard error

array_same() does not write to standard error.

array_sort()

Sort an array.

Synopsis

array_sort "${array[@]}"

Description

The array_sort() function sorts the elements of array and outputs the result to standard output. The elements are sorted using a natural number sort, similar to the sort -V command from GNU Coreutils. If the array is empty, nothing will be written to standard output.

Return value

Return value Meaning
0 Success (always returned)

Standard input

array_sort() does not read from standard input.

Standard output

The sorted array is written to standard output, one element per line.

Standard error

array_sort() does not write to standard error.

array_to_lines()

Print an array, one element per line.

Synopsis

array_to_lines "${array[@]}"

Description

The array_to_lines() function outputs array to standard output, writing each element in the array to a separate line. The elements of the array are output as-is, meaning that an empty string in the array will cause an empty line to be written. The elements will be written in their order in the array. If the array is empty, nothing will be written to standard output.

Return value

Return value Meaning
0 Success (always returned)

Standard input

array_to_lines() does not read from standard input.

Standard output

array_to_lines() writes one line per array element to standard output. If the array is empty, nothing will be written.

Standard error

array_to_lines() does not write to standard error.