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


Groups > gnu.bash.bug > #15723

Re: Two states of empty arrays

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Martin Schulte <gnu@schrader-schulte.de>
Newsgroups gnu.bash.bug
Subject Re: Two states of empty arrays
Date Thu, 12 Dec 2019 22:34:52 +0100
Lines 37
Approved bug-bash@gnu.org
Message-ID <mailman.751.1576186508.1979.bug-bash@gnu.org> (permalink)
References <c9224e23-ff7d-ba84-dc4f-aa68db902f72@noiraude.net> <79b6365d-4c09-7122-25c3-4564fd304948@case.edu> <mailman.740.1576178010.1979.bug-bash@gnu.org> <qsua0m$fkh$1@solo.fdn.fr> <20191212223452.157114bb8c51cb2689b793b8@schrader-schulte.de>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace usenet.stanford.edu 1576186508 3351 209.51.188.17 (12 Dec 2019 21:35:08 GMT)
X-Complaints-To action@cs.stanford.edu
Cc gnu-bash-bug@moderators.isc.org
To Léa Gris <lea.gris@noiraude.net>
Envelope-to bug-bash@gnu.org
Delivered-To gnu-bash-bug@moderators.isc.org
In-Reply-To <qsua0m$fkh$1@solo.fdn.fr>
X-Mailer Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2001:19f0:5c01:c22:5400:1ff:fead:27ae
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <20191212223452.157114bb8c51cb2689b793b8@schrader-schulte.de>
X-Mailman-Original-References <c9224e23-ff7d-ba84-dc4f-aa68db902f72@noiraude.net> <79b6365d-4c09-7122-25c3-4564fd304948@case.edu> <mailman.740.1576178010.1979.bug-bash@gnu.org> <qsua0m$fkh$1@solo.fdn.fr>
Xref csiph.com gnu.bash.bug:15723

Show key headers only | View raw


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

Back to gnu.bash.bug | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web