Skip to main content
added 102 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

Note that in the general case, you'd want to loop over "${array[@]}" rather than ${array[*]} as using [*] (and leaving the expansion unquoted) would do both word splitting and filename globbing on the strings in the array. The expression "${array[@]}" would expand to the individually quoted elements of the array array.

Avoiding word splitting and filename globbing is also the reason for quoting both $elem and $level when comparing them etc.


A possibly quicker way to do this would be to first create an associative array with the element from the array as keys and then just do key lookups in that:

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

array=( low medium none high )

typeset -A lookup

for elem in "${array[@]}"; do
    lookup["$elem"]=1
done

for level in high medium low none; do
    if [ -n "${lookup[$level]}" ]; then
        "$level"
        break 
    fi
done

This avoids the double loop, which would be useful if either the array or the number of level, or both, are very long.

I've also switched to the more common =(...) assignment form for arrays.

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

Note that in the general case, you'd want to loop over "${array[@]}" rather than ${array[*]} as using [*] (and leaving the expansion unquoted) would do both word splitting and filename globbing on the strings in the array.


A possibly quicker way to do this would be to first create an associative array with the element from the array as keys and then just do key lookups in that:

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

array=( low medium none high )

typeset -A lookup

for elem in "${array[@]}"; do
    lookup["$elem"]=1
done

for level in high medium low none; do
    if [ -n "${lookup[$level]}" ]; then
        "$level"
        break 
    fi
done

This avoids the double loop, which would be useful if either the array or the number of level, or both, are very long.

I've also switched to the more common =(...) assignment form for arrays.

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

Note that in the general case, you'd want to loop over "${array[@]}" rather than ${array[*]} as using [*] (and leaving the expansion unquoted) would do both word splitting and filename globbing on the strings in the array. The expression "${array[@]}" would expand to the individually quoted elements of the array array.

Avoiding word splitting and filename globbing is also the reason for quoting both $elem and $level when comparing them etc.


A possibly quicker way to do this would be to first create an associative array with the element from the array as keys and then just do key lookups in that:

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

array=( low medium none high )

typeset -A lookup

for elem in "${array[@]}"; do
    lookup["$elem"]=1
done

for level in high medium low none; do
    if [ -n "${lookup[$level]}" ]; then
        "$level"
        break 
    fi
done

This avoids the double loop, which would be useful if either the array or the number of level, or both, are very long.

I've also switched to the more common =(...) assignment form for arrays.

added 607 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done
#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

Note that in the general case, you'd want to loop over "${array[@]}" rather than ${array[*]} as using [*] (and leaving the expansion unquoted) would do both word splitting and filename globbing on the strings in the array.


A possibly quicker way to do this would be to first create an associative array with the element from the array as keys and then just do key lookups in that:

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

array=( low medium none high )

typeset -A lookup

for elem in "${array[@]}"; do
    lookup["$elem"]=1
done

for level in high medium low none; do
    if [ -n "${lookup[$level]}" ]; then
        "$level"
        break 
    fi
done

This avoids the double loop, which would be useful if either the array or the number of level, or both, are very long.

I've also switched to the more common =(...) assignment form for arrays.

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.

Note that in the general case, you'd want to loop over "${array[@]}" rather than ${array[*]} as using [*] (and leaving the expansion unquoted) would do both word splitting and filename globbing on the strings in the array.


A possibly quicker way to do this would be to first create an associative array with the element from the array as keys and then just do key lookups in that:

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

array=( low medium none high )

typeset -A lookup

for elem in "${array[@]}"; do
    lookup["$elem"]=1
done

for level in high medium low none; do
    if [ -n "${lookup[$level]}" ]; then
        "$level"
        break 
    fi
done

This avoids the double loop, which would be useful if either the array or the number of level, or both, are very long.

I've also switched to the more common =(...) assignment form for arrays.

Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

#!/bin/ksh

high ()   echo High
medium () echo Medium
low ()    echo Low
none ()   echo None

set -A array low medium none high

for level in high medium low none; do
    for elem in "${array[@]}"; do
        if [ "$elem" = "$level" ]; then
            "$level"
            break 2
        fi
    done
done

This is a double loop. The outer loop loops over the levels in order of importance. The inner loop loops over the elements in the array, looking for an element that is equal to the current level.

If an element is found that corresponds to the current level, the function with the same name as the level is called and the two loops are exited.