help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] indirection for arrays


From: Steven W. Orr
Subject: Re: [Help-bash] indirection for arrays
Date: Mon, 07 May 2012 09:12:52 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20

On 5/7/2012 8:22 AM, Greg Wooledge wrote:
On Sat, May 05, 2012 at 02:16:36PM -0600, Bill Gradwohl wrote:
Via a web site reference, Dan Douglas (Thank You) provided something akin
to this:

func() {
     local -a 'keys=("${!'"$1"'address@hidden")'

It burns!!

I love this! Greg, this should go down as historic material. :-)>

Seriously though... I went through a *lot* of research to get to the material that I use on a regular basis to accomplish passing variables (both scalars and arrays) by reference. It's not a *great* solution, it's not for the faint of heart, and collisions are possible.

http://fvue.nl/wiki/Bash:_Passing_variables_by_reference

But! This stuff works and if you think you want to modify it to make it better, I encourage you to do so. Not because there's anything to fix, but because if you try to fix anything you'll probably end up learning the nuances (and subtle nuances they are!) of *exactly* how this gem works.

Once I got this working, I ended up writing a wrapper called glob_array that takes pairs of array names and globbing patterns. The arrays are then set to the result of the globs.

So now I can say stuff like:

upvar $1 $value

and I can also say stuff like:

typeset -a flist
glob_array flist "$srpms/$dist/elx-lpfc-*_$dist.src.rpm"
(( ${#flist[#]} == 0 )) && die 'No src rpms.'



declare -a myArray=(1 2 3)

func 'myArray'

For the love of god, if you want to pass arrays to a function by name,
use ksh93, not bash.

arc3:~$ cat foo
#!/bin/ksh
foo() {
   nameref array="$1"
   array[42]=forty-two
}
foo bar
for i in "address@hidden"; do
   echo "bar[$i]=${bar[i]}"
done
arc3:~$ ./foo
bar[42]=forty-two

UNTIL SUCH TIME as bash has a feature like this, you cannot do this in
bash.  Throwing dozens of random punctuation characters at the screen
until the code appears to do what you think it should do is not sane.


--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



reply via email to

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