Re: safely iterating through potentially empty arrays in bash
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 02/27/04
- Next message: Mike Chirico: "sed problem...simpler solution?"
- Previous message: Dale DeRemer: "Remove first comma in a file"
- In reply to: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Next in thread: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Reply: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Feb 2004 22:17:39 +0100
2004-02-27, 20:19(+00), William Park:
[...]
> You are missing nuance. $a is asking is 'a' defined; answer is yes,
> because at least one item (a[1]) is defined. ${a[0]} is asking is
> 'a[0]' defined; answer is no.
Nuances are not consistent.
With "set -u" is meant to exit if accessing an unset variable
${a-replace} is supposed to expand to "replace" if $a in unset.
Nuances don't match in the case of a[1]=2
>> zsh doesn't have problems because in zsh, array and scalar
>> are two distincts types of variable (and anyway, zsh arrays are
>> not hashes as in bash or ksh).
>
> In Bash, array variable names are hashed, like any other shell
> variables. But, array itself (ie. data in array) is circular linked
> list
I meant that bash (and ksh) arrays are not arrays as in every
other language (C, Java, perl, zsh...) (and I'm not speaking of
the interpreter internal implementaion), but hashes with keys
restricted to the interger type.
The positional parameters are an array in bash, not bash arrays.
That may be why they are not mapped to a bash array, while they
are in zsh (argv).
in zsh:
a[100]=1
creates an array of size 100 (indices start at 1 in zsh like the
positional parameters).
In bash or ksh:
a[100]=1
creates a hash (or associative array if you prefer) of size "1",
with the value 1 associated to the key 100.
Note that zsh also has associative arrays, but keys are not
restricted to the integer type and the array type is different from
the hash type:
$ typeset -A h
$ typeset -a a
$ h[foo]=bar a[12]=baz
$ echo ${#h}
1
$ echo ${#a}
12
Note that it is very tricky in bash to know the keys of its
(associative) arrays, you need something like:
get_keys() {
local IFS=" "
REPLY=
local fopt=
[[ "$-" = *f* ]] || fopt="set +f"
set -f
eval "set -- $(declare -p -- "$1" 2> /dev/null)"
[[ "$2" = -*a* ]] || eval "$fopt; return 1"
eval 'REPLY=${'"$#"'#*"=("}'
eval "set -- ${REPLY%')'}"
set -- "${@%%']'*}"
REPLY=${*#'['}
eval "$fopt"
}
$ a[4]=4
$ a[12]=12
$ get_keys a
$ echo "$REPLY"
4 12
In ksh93:
$ a[4]=4
$ a[12]=12
$ echo "${!a[@]}"
4 12
In zsh:
$ typeset -A h
$ h[foo]=12
$ h[bar]=4
$ echo ${(k)h}
foo bar
-- Stéphane ["Stephane.Chazelas" at "free.fr"]
- Next message: Mike Chirico: "sed problem...simpler solution?"
- Previous message: Dale DeRemer: "Remove first comma in a file"
- In reply to: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Next in thread: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Reply: William Park: "Re: safely iterating through potentially empty arrays in bash"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|