Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to check for an index or a key in an array?

Using:

set -o nounset 
  1. 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.

  2. 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?

  3. 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?

like image 919
Luca Borrione Avatar asked Nov 04 '12 14:11

Luca Borrione


People also ask

How do you check if a key is in an array?

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.

How do you find the index of an object in an array?

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.

How do I check in Javascript if a value exists at a certain array index?

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.

How to check if an array contains a specific key/index?

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 $...

How to find the index of an element in a stream?

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?

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.

How to binary search an array in Java?

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.


1 Answers

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

  • if array[key] is set, return abc
  • if array[key] is not set, return nothing

References:
  1. See Parameter Expansion in Bash manual and the little note

if the colon is omitted, the operator tests only for existence [of parameter]

  1. This answer is actually adapted from the answers for this SO question: How to tell if a string is not defined in a bash shell script?

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  
like image 88
doubleDown Avatar answered Oct 24 '22 02:10

doubleDown