bug-bash
[Top][All Lists]
Advanced

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

Re: need ability to tell if array is associative or not - bug?


From: Jason Vas Dias
Subject: Re: need ability to tell if array is associative or not - bug?
Date: Fri, 29 Aug 2014 16:45:30 +0100

Actually, this appears to be a bit more involved.
What I was actually trying to work out was
the bash error that occurs with :

$( function f()
{
    local an_array=$1;
    local value='1.0';
    local v=value;
    local ev='['"'"'value'"'"']='"'""${!v}""'";
    eval ${an_array}='('"$ev"')';
}
  declare -a my_array;
  set -x; f my_array;
)
+ f my_array
+ local an_array=my_array
+ local value=1.0
+ local v=value
+ local 'ev=['\''value'\'']='\''1.0'\'''
+ eval 'my_array=(['\''value'\'']='\''1.0'\'')'
++ my_array=(['value']='1.0')
bash: 1.0: syntax error: invalid arithmetic operator (error token is ".0")

This error does not happen if the array was originally declared associative:

$ ( function f()  {       local an_array=$1;     local value='1.0';
 local v=value;     local ev='['"'"'value'"'"']='"'""${!v}""'";
eval ${an_array}='('"$ev"')'; };
declare -A my_array; set -x; f my_array )
+ f my_array
+ local an_array=my_array
+ local value=1.0
+ local v=value
+ local 'ev=['\''value'\'']='\''1.0'\'''
+ eval 'my_array=(['\''value'\'']='\''1.0'\'')'
++ my_array=(['value']='1.0')

Nor does the error happen if indirect expansion is not used:

$ ( function f()  {       local an_array=$1;     local value='1.0';
     local v=$value;
     local ev='['"'"'value'"'"']='"'""$v""'";
     eval ${an_array}='('"$ev"')'; }; declare -A my_array; set -x; f my_array )
+ f my_array
+ local an_array=my_array
+ local value=1.0
+ local v=1.0
+ local 'ev=['\''value'\'']='\''1.0'\'''
+ eval 'my_array=(['\''value'\'']='\''1.0'\'')'
++ my_array=(['value']='1.0')
$

So I was wondering if there is any way to determine if an array was originally
declared associative or not.

But it appears that there is a bash bug here that is triggered only if the
array was originally declared not associative and an indirect expansion
is involved in setting an array member.

The end result expression being evaluated:
    ++ my_array=(['value']='1.0')
should never involve an arithmetic expression,
and should be valid regardless if the array is
associative or not .

Any ideas what might be going on here ?

Thanks in advance,
Jason

On 8/29/14, Jason Vas Dias <jason.vas.dias@gmail.com> wrote:
> Sorry, mailer sent previous mail before I was ready. Reposting.
>
> Good day list -
>
> There seems to be no way of testing if an array variable is associative or
> not ,
>
> I have something like:
>
>     declare -xr TYPE_ARRAY=0 TYPE_ASSOC=1
>     function f()
>     {  declare -n an_array=$1;
>        local type=$2;
>        case $type in
>          $TYPE_ASSOC)
>               an_array['some_value']=1;
>               ;;
>          $TYPE_ARRAY)
>               an_array[0]=1;
>        esac
>    }
>
> Now, if I call :
>    declare -a my_array();  f my_array $TYPE_ASSOC;
> I'll end up with no 'some_value' subscript in array.
>
> It would be great if bash could provide some '-A'  conditional
> expression operator
> to test if a variable is an associative array or not .
> Or perhaps 'declare -A identifier' could return non-zero if
> 'identifier' was not previously defined as an associative array, as
> declare -F does for functions ?
> Or is there some way to test if a variable is an associative array or not?
>
> Thanks & Regards,
> Jason
>
>
> On 8/29/14, Jason Vas Dias <jason.vas.dias@gmail.com> wrote:
>> Good day list -
>>
>> There seems to be no way of testing if an array variable is associative
>> or
>> not ,
>> yet attempting to make associative assigments to a normal array results
>> in
>> a
>> syntax error .
>>
>> I have something like:
>>
>>     declare -xr TYPE_ARRAY=0 TYPE_ASSOC=1
>>     function f()
>>     {  declare -n an_array=$1;
>>        local type=$2;
>>        case $type in
>>          $TYPE_ASSOC)
>>               an_array['some_value']=1;
>>
>>
>>     }
>>
>



reply via email to

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