Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15717 > unrolled thread

Re: Two states of empty arrays

Started byChet Ramey <chet.ramey@case.edu>
First post2019-12-12 14:13 -0500
Last post2019-12-12 22:34 +0100
Articles 3 — 3 participants

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Two states of empty arrays Chet Ramey <chet.ramey@case.edu> - 2019-12-12 14:13 -0500
    Re: Two states of empty arrays Léa Gris <lea.gris@noiraude.net> - 2019-12-12 22:04 +0100
    Re: Two states of empty arrays Martin Schulte <gnu@schrader-schulte.de> - 2019-12-12 22:34 +0100

#15717 — Re: Two states of empty arrays

FromChet Ramey <chet.ramey@case.edu>
Date2019-12-12 14:13 -0500
SubjectRe: Two states of empty arrays
Message-ID<mailman.740.1576178010.1979.bug-bash@gnu.org>
On 12/12/19 12:08 PM, Léa Gris wrote:
> Hello,
> 
> Depending on how an empty array is declared, it is not stored with the
> same state.
> 
> # Empty array declared without parenthesis
> unset myArr
> declare -a myArr
> typeset -p myArr
> echo "${#myArr[@]}"

This is an unset variable with the array attribute; you have not assigned a
value.


> # Empty array declared without parenthesis
> unset myArr
> declare -a myArr=()

This is an empty array variable; you have assigned a value.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

[toc] | [next] | [standalone]


#15722

FromLéa Gris <lea.gris@noiraude.net>
Date2019-12-12 22:04 +0100
Message-ID<mailman.749.1576184670.1979.bug-bash@gnu.org>
In reply to#15717
Le 12/12/2019 à 20:13, Chet Ramey écrivait :

>> # Empty array declared without parenthesis
>> unset myArr
>> declare -a myArr
>> typeset -p myArr
>> echo "${#myArr[@]}"
> 
> This is an unset variable with the array attribute; you have not assigned a
> value.
>> # Empty array declared without parenthesis
>> unset myArr
>> declare -a myArr=()
> 
> This is an empty array variable; you have assigned a value.

Thank you and Clint, it makes sense now.

I was trying to play the the -v test to detect when an array or
associative array has been declared, not necessarily assigned entries
key, values, to not error when Bash runs with -o nounset

Like here:
#!/usr/bin/bash
set -o nounset
myArr+=(["key"]="value")

ERR: line 3: key: unbound variable

I can test the type of myArr this way:
if [[ "$(typeset -p myArr 2>&1)" =~ ^declare\ -A ]]; then
  myArr+=(["key"]="value")
fi

But it looks sub-optimal to test the type and declaration of a variable.
The -v test flag cannot be used because it requires the associative
array to contain at least a [key]=value entry as mentioned in the man
bash.1:

>        -v varname
>               True if the shell variable varname is set (has been assigned a value).

_has been assigned a value_

-- 
Lea Gris

[toc] | [prev] | [next] | [standalone]


#15723

FromMartin Schulte <gnu@schrader-schulte.de>
Date2019-12-12 22:34 +0100
Message-ID<mailman.751.1576186508.1979.bug-bash@gnu.org>
In reply to#15717
Hello Léa!

Léa Gris <lea.gris@noiraude.net> wrote:
> I was trying to play the the -v test to detect when an array or
> associative array has been declared, not necessarily assigned entries
> key, values, to not error when Bash runs with -o nounset

Just for the curious: What is your attention here?

I think that most useful questions (Is there an element in the array? Is
there a value for a given key?) can be answered in a simpler way:

#!/bin/bash

set -o nounset

# From what I learned today it seems to be good practice to always
# assign and empty array to when declaring an associative array:
declare -A assoc=()

echo ${#assoc[@]} # Are there elements in it?

assoc[key1]=val1
assoc[key2]=

for key in key1 key2 key3; do
  if [[ -n ${assoc[$key]+isset} ]]; then
    echo "Element for $key is set"
  else
    echo "No Element for $key"
  fi
done

Best regards,

Martin

[toc] | [prev] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web