Using:
set -o nounset
Having an indexed array like:
myArray=( "red" "black" "blue" )
What is the shortest way to check if element 1 is set?
I sometimes use the following:
test "${#myArray[@]}" -gt "1" && echo "1 exists" || echo "1 doesn't exist"
I would like to know if there's a preferred one.
How to deal with non-consecutive indexes?
myArray=() myArray[12]="red" myArray[51]="black" myArray[129]="blue"
How to quick check that 51
is already set for example?
How to deal with associative arrays?
declare -A myArray myArray["key1"]="red" myArray["key2"]="black" myArray["key3"]="blue"
How to quick check that key2
is already used for example?
You can use the PHP array_key_exists() function to test whether a given key or index exists in an array or not. This function returns TRUE on success or FALSE on failure.
To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.
The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.
Method 2: Using isset () Method: The isset () function checks whether a specific key or index is present inside an array or not. bool isset ( mixed $var, mixed $...
The stream represents a sequence of objects from a source, which supports aggregate operations. In order to find the index of an element Stream package provides utility, IntStream. Using the length of an array we can get an IntStream of array indices from 0 to n-1, where n is the length of an array.
How to check a key exists in an array in PHP ? We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP. The problem can be solved using PHP inbuilt function for checking key exists in a given array.
But the binary search can only be used if the array is sorted. Java provides us with an inbuilt function which can be found in the Arrays library of Java which will return the index if the element is present, else it returns -1. The complexity will be O (log n). Below is the implementation of Binary search. 3.
To check if the element is set (applies to both indexed and associative array)
[ "${array[key]+abc}" ] && echo "exists"
Basically what ${array[key]+abc}
does is
array[key]
is set, return abc
array[key]
is not set, return nothingif the colon is omitted, the operator tests only for existence [of parameter]
A wrapper function:
exists(){ if [ "$2" != in ]; then echo "Incorrect usage." echo "Correct usage: exists {key} in {array}" return fi eval '[ ${'$3'[$1]+muahaha} ]' }
For example
if ! exists key in array; then echo "No such array element"; fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With