bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: test '-v' - associative vs. normal array discrepancy - a bug ?


From: Piotr Grzybowski
Subject: Re: test '-v' - associative vs. normal array discrepancy - a bug ?
Date: Wed, 19 Nov 2014 20:14:24 +0100

hi,

the following makes -v return true for non-empty associative arrays,
what do you think?

diff --git a/test.c b/test.c
index ab7bec7..8a91d1e 100644
--- a/test.c
+++ b/test.c
@@ -638,9 +638,13 @@ unary_test (op, arg)
        }
       else if (v && invisible_p (v) == 0 && assoc_p (v))
        {
-         char *t;
-         t = assoc_reference (assoc_cell (v), "0");
-         return (t ? TRUE : FALSE);
+         HASH_TABLE *h=assoc_cell (v);
+         if (h && h->nentries>0)
+        {
+         return (TRUE);
+        } else {
+         return (FALSE);
+        }
        }
 #endif
       return (v && invisible_p (v) == 0 && var_isset (v) ? TRUE : FALSE);

cheers,
pg



On Wed, Nov 19, 2014 at 5:39 PM, Greg Wooledge <address@hidden> wrote:
> On Wed, Nov 19, 2014 at 04:20:51PM +0000, Jason Vas Dias wrote:
>> Good day -
>> Please could anyone explain why the first command below produces no output:
>> $ ( declare -A a=([a]=1);   if [ -v a ]; then echo yes; fi )
>> $ ( declare -a  a=([0]=1);  if [ -v a ]; then echo yes; fi )
>> yes
>
> In a lot of places, when you use the name of an array without an index,
> Bash assumes you mean index 0.
>
> imadev:~$ unset a; declare -a a=([1]=1); test -v a && echo yes
> imadev:~$ unset a; declare -A a=([0]=1); test -v a && echo yes
> yes
>
> So it's not indexed vs. associative arrays.  It's simply the fact that you
> used index a instead of index 0, plus the fact that -v a is really -v 'a[0]'.
>
> Does this make -v unusable for arrays?  Possibly.
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]