L A Walsh
2018-09-26 00:17:27 UTC
It struck me as it might be convenient if 'shift' could take an optional
arrayname as an argument. Would that be possible or would it cause some
incompatibility?
i.e.
I know it can be done with a function, but with more mess.
I used (maybe there's a better way, but...):
(in my lib file ArFuncs.shh, that I can include)
[include stdalias]
#[include Types] #if type-checking include Types+line below
lshift () {
(($#)) || return 1
int nshift=1
if [[ $1 =~ ^[0-9]+$ ]]; then nshift=$1; shift;fi
#if ! isArr $1; then echo >&2 "Need arrayname"; return 1; fi
my ar=$1; shift
my h="$ar[@]"
set "${!h}"
shift $nshift
eval "${ar}=("$@")"
}; export -f lshift
arrayname as an argument. Would that be possible or would it cause some
incompatibility?
i.e.
set one two three four five
shift ARGV
two three four fiveshift ARGV
shift 2 ARGV
four fiveI know it can be done with a function, but with more mess.
I used (maybe there's a better way, but...):
(in my lib file ArFuncs.shh, that I can include)
[include stdalias]
#[include Types] #if type-checking include Types+line below
lshift () {
(($#)) || return 1
int nshift=1
if [[ $1 =~ ^[0-9]+$ ]]; then nshift=$1; shift;fi
#if ! isArr $1; then echo >&2 "Need arrayname"; return 1; fi
my ar=$1; shift
my h="$ar[@]"
set "${!h}"
shift $nshift
eval "${ar}=("$@")"
}; export -f lshift