0

Below, I have the demonstration of, and the listing of, functions in bash (4.2.46) for array manipulation - push, pop, shift, unshift. You'll notice that even though I pass the name of the array into the functions and assign it to the variable array_name, the function only works with the hardcoded array array. This is because I've been wraking my brain, my fingers, and the internet to no avail trying to determine the magic indirect incantation notation that would allow me to use array_name instead, and be able to modify any array in these functions. Would anyone like to take a crack at it?

Thanks

UPDATE. This question was marked as as a duplicate. I used that question to solve my issue. And I repointed my shell to use Bash 4.3+.

Below now is the SOLUTION to my issue of using array variable indirection:

$ foo=

$ foo=()

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**

$ apush foo "item 1"

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1

$ apush foo "item 2"

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2

$ apush foo "item 3"

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2
item 3

$ aunshift foo "item 0"

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 0
item 1
item 2
item 3

$ apop foo
item 3

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 0
item 1
item 2

$ ashift foo
item 0

$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2

## push, pop, shift, unshift:
## http://www.tech-recipes.com/rx/911/queue-and-stack-using-array/
## http://stackoverflow.com/questions/12691121/bash-array-expansion-using-variable-indirection-expansion
## http://stackoverflow.com/questions/23819839/assign-to-a-bash-array-variable-indirectly-by-dynamically-constructed-variable

##bash 4.3+: use declare -n to effectively create an alias ('nameref') of another variable

aclear ()
{
    declare -n array_alias="$1"
    array_alias=()
}

apush ()
{
    declare -n array_alias="$1"
    local item="$2"
    local count=${#array_alias[@]}
    ## index = 0 to count-1. Next element is [count]
    array_alias[$count]="$item"
}

alist ()
{
    local use_idx=0
    if [ "$1" = '--idx' ]
    then
        use_idx=1
        shift
    fi

    declare -n array_alias="$1"
    if [ "$use_idx" = 1 ]
    then
        for i in "${!array_alias[@]}"
        do
            printf "%s\t%s\n" "$i" "${array_alias[$i]}"
        done
    else
        for i in "${array_alias[@]}"
        do
            echo $i
        done
    fi
}

apop ()
{
    declare -n array_alias="$1"
    local item=${array_alias[((${#array_alias[@]}-1))]}
    array_alias=("${array_alias[@]:0:$((${#array_alias[@]}-1))}")
    echo $item
}

ashift ()
{
    declare -n array_alias="$1"
    local item=${array_alias[0]}
    array_alias=("${array_alias[@]:1}")
    echo $item
}

aunshift ()
{
    declare -n array_alias="$1"
    local item="$2"
    local i
    local j
    for (( i = ${#array_alias[@]} ; i > 0 ; i-- ))
    do
        ((j=i-1))
        array_alias[$i]="${array_alias[$j]}"
    done
    array_alias[0]="$item"
}

adelval ()
{
    declare -n array_alias="$1"
    local item=$2
    local i
    for (( i = 0 ; i < ${#array_alias[@]} ; i++ ))
    do
        if [ "$item" = "${array_alias[$i]}" ]
        then
            break
        fi
    done
    adelidx $i
}

adelidx ()
{
    declare -n array_alias="$1"
    local idx=$2
    array_alias=("${array_alias[@]:0:$idx}" "${array_alias[@]:$(($idx + 1))}")
}
mpersico
  • 766
  • 7
  • 19
  • I'm not sure what you expect to happen. If you use `array=`, it assigns to the variable named `array`. What does the `array_name` variable have to do with it? – Barmar Jun 10 '16 at 18:40
  • 1
    None of your code uses the `!` indirection operator. But that can only be used when reading from a variable, it can't used to assign to a variable. – Barmar Jun 10 '16 at 18:44
  • Yes, I know I am not using indirection, but I wanted to post working code. All that is left is to ADD the indirection. That's what I was hoping to get advice on - how to assign to an array using indirect notation. – mpersico Jun 10 '16 at 18:48
  • I hope the duplicate question I linked to helps you. – Barmar Jun 10 '16 at 18:50
  • You know, I found that, but it is for individual assignments and the code I had wasn't doing that. However, a second look at both leads me to believe I could probably just change my code to do individual assignments. I will have to do some more assigning around which might make the code a little more inefficient. However, it should be much safer for assignments that contain spaces in them, Thank you for your help. When complete, I will post a solution for reference. – mpersico Jun 10 '16 at 19:00

0 Answers0