[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 <maroloccio@gmail.com> 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" "${a[@]}"
>
> 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 ((${#a[@]})); do
pop a v
declare -p a v
done
Re: Add support for array .pop(), Chet Ramey, 2016/10/15