[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] array issue
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] array issue |
Date: |
Tue, 21 Feb 2012 09:44:44 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Feb 21, 2012 at 08:06:36AM -0600, address@hidden wrote:
> I have used $((address@hidden - 1)) to get the highest subscript
> Is there a better/easier way
That doesn't give the highest subscript if the array is sparse (has holes).
The real question is, WHY are you trying to get the highest subscript? If
you simply want to append to an array, and you're in bash 3.1 or higher,
you can use +=
array+=("my new element")
imadev:~$ a=([0]=zero [1]=one [10]=ten [11]=eleven)
imadev:~$ a+=(foo)
imadev:~$ set | grep ^a=
a=([0]="zero" [1]="one" [10]="ten" [11]="eleven" [12]="foo")
If you actually need the value of the highest index for some bizarre
reason, then the only safe way to retrieve it is to pull the entire list
of indices using the address@hidden syntax (requires bash 3.0 or higher):
imadev:~$ echo "address@hidden"
0 1 10 11 12
There is NO way to get the value of the highest index in bash 2.