bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Add support for array .pop()


From: lolilolicon
Subject: Re: Add support for array .pop()
Date: Sat, 15 Oct 2016 17:41:32 +0800

On Sat, Oct 15, 2016 at 11:12 AM, Marco Ippolito <address@hidden> wrote:
> Bash has elegant and powerful constructs like `mapfile',
> yet it is missing something as easy as an array "pop".
>
> Extract the last value of an array at the same time as
> removing it from the array.
>
> Is this the best one can do?
>
> $ a=(1 2 3); v=${a[-1]}; unset 'a[-1]'; printf '%s\n' "$v" "address@hidden"
>
> The 2-step approach gets tiresome after a while.

Does this help?

pop() {
    local -n _a=$1
    printf -v "$2" "${_a[-1]}"
    unset _a[-1]
}

declare -a a=(a b c)
while ((address@hidden)); do
    pop a v
    declare -p a v
done



reply via email to

[Prev in Thread] Current Thread [Next in Thread]