How to get the next element in a sparse array?

mickp

I need to get the next element of an array. For contiguous arrays, my code works just fine:

arr=(item{2..5})
value=item3

for ((i=0;i<${#arr[@]};++i)); do
    if [[ ${arr[i]} = "$value" ]] && (( i+1<${#arr[@]} )); then
        echo "${arr[i+1]}"
        break
    fi  
done

I didn't find any definition of how parameter expansion behaves on sparse arrays and this code unfortunately doesn't yield the desired result either:

arr=([3]=item2 [7]=item3 [10]=item4 [1]=item5)

for ((i=0;i<${#arr[@]};++i)); do
    if [[ ${arr[@]:i:1} = "$value" ]]; then
        ...
        ...
    fi
done

How can I do this for sparse arrays (possibly without the need to copy the array as to reindex it) ?

Cyrus
#!/bin/bash

arr=([3]=item2 [7]=item3 [10]=item4 [1]=item5)
value="item3"
switch="0"

# loop over array's indexes
for i in "${!arr[@]}"; do
  if [[ $switch = "1" ]]; then
    echo "found ${arr[$i]} in element $i"
    switch="0"
    break
  fi

  if [[ ${arr[$i]} = "$value" ]]; then
    echo "found ${arr[$i]} in element $i"
    switch="1"
    continue
  fi
done

Output:

found item3 in element 7
found item4 in element 10

The script does not check whether the end of the array has already been reached.


See: help continue

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get next element in a sparse array

How to get the next element after the .active class

How to get the next element on Map function?

How to get next Element/siblings in Angular 2

How to get <span> element to align with element next to it?

How to get indices of diagonal elements of a sparse matrix data array

How to delete an element from a sparse array?

How to get element as an array?

How to get the next and previous element in an array, Ruby

How can I get the next element of an array in C?

How to get the next element from monix observable?

How to get the index of element in an array and return the next element in Hive?

How to find the next element given a certain element in array

How to get the next hash element from hash?

Get next array element

Jquery : how to get name of next element

php how to get next element in the array and loop again when ended?

Get value of next element in array of arrays

PHP - get array value if next element is not series

how to multiply the array element in to the next element and output as an array in Swift

Get the index of the first or nth element of sparse bash array

How can I get next few elements in an array but jump back to the start when passing the last element?

How to get Previous and Next element of a list in python

Python, Selenium: How to get text next to element

how to get element that equal or greater than X but smaller than the next X in array?

How to get paragraph element to next line

How to get next and previous five element of from an array?

How to get next sequence of array?

Get the next element of an array