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: Chet Ramey
Subject: Re: need ability to tell if array is associative or not - bug?
Date: Fri, 29 Aug 2014 11:21:18 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0

On 8/29/14, 10:21 AM, Jason Vas Dias 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;

Syntax errors aside, isn't this pretty clearly a programming error?  I
mean, you've passed the wrong type.

I can maybe see the value in being able to write code that is able to
catch these kinds of errors without parsing the output of `declare', but
it's a big stretch to call this a bug.

> 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 ?

It already does that when you attempt to convert an indexed array to an
associative array, and prints an error message to drive the point home.
The problem with doing it in the general case is that the semantics of
converting a scalar to an associative array are already well-defined.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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