Re: Add support for array .pop()

2016-10-15 Thread Chet Ramey
On 10/14/16 11:12 PM, Marco Ippolito wrote: > Bash has elegant and powerful constructs like `mapfile', > yet it is missing something as easy as an array "pop". There is source code support for operations like pop and similar. If you think it's worthwhile, you might look at writing a loadable buil

Re: Add support for array .pop()

2016-10-15 Thread lolilolicon
On Sat, Oct 15, 2016 at 10:50 PM, konsolebox wrote: > > My version would be this: > > function array_pop { declare -n __a=$1 __v=$2; __v=${__a[-1]}; unset > '__a[-1]'; } > Here's another version which avoids local or nameref variables, pop() { [[ -v $1 ]] || return set -- "$1[-1]" "$2"

Re: Add support for array .pop()

2016-10-15 Thread konsolebox
On Sat, Oct 15, 2016 at 7:58 PM, Marco Ippolito wrote: > On Sat, Oct 15, 2016 at 07:32:23PM +0800, konsolebox wrote: >> On Sat, Oct 15, 2016 at 11:12 AM, Marco Ippolito >> wrote: >> > Bash has elegant and powerful constructs like `mapfile', >> > yet it is missing something as easy as an array "p

Re: Add support for array .pop()

2016-10-15 Thread lolilolicon
On Sat, Oct 15, 2016 at 7:45 PM, Marco Ippolito wrote: > On Sat, Oct 15, 2016 at 05:41:32PM +0800, lolilolicon wrote: >> 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

Re: Add support for array .pop()

2016-10-15 Thread lolilolicon
On Sat, Oct 15, 2016 at 5:41 PM, lolilolicon wrote: > > pop() { > local -n _a=$1 > printf -v "$2" "${_a[-1]}" and of course I meant printf -v "$2" '%s' "${_a[-1]}" The keyword is nameref if you haven't heard of it, so you can look it up in the man page :)

Re: Add support for array .pop()

2016-10-15 Thread lolilolicon
On Sat, Oct 15, 2016 at 11:12 AM, Marco Ippolito 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? > >

Re: Add support for array .pop()

2016-10-15 Thread konsolebox
On Sat, Oct 15, 2016 at 11:12 AM, Marco Ippolito 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? > >

Add support for array .pop()

2016-10-15 Thread Marco Ippolito
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[@]}" Th