BLU Discuss list archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
passing arrays and working with arrays in BASH
- Subject: passing arrays and working with arrays in BASH
- From: tmetro-blu-5a1Jt6qxUNc at public.gmane.org (Tom Metro)
- Date: Fri, 26 Mar 2010 18:31:12 -0400
- In-reply-to: <1269637953.6858.17.camel@corelux>
- References: <1269637953.6858.17.camel@corelux>
theBlueSage wrote: > ------------ start code snippet -------------- > #!/bin/bash > function search_array() { > index=0 > array=( "$@" ) > #echo ${array[@]} > let TCNT=${#array[@]}-1 > LASTVAL=${array[${#array[@]}-1]} > while [ "$index" -lt "$TCNT" ]; do > if [ "${array[$index]}" = "$LASTVAL" ]; then > echo $index > return > fi > let "index++" > done > echo "" > } > -------- end code snippet ---------------- > > The result is I add the scalar to the end of the array, pass it to > the function and then peel it off again. It seems like you are seeking a pass-by-reference mechanism, of which I'm not aware of a way to do that in Bash. But there are two alternatives that you may consider a slight improvement: 1. When ever you have a function that has fixed arguments plus a variable length list, put the fixed arguments first, so you can use 'shift' to pull them off the argument list, leaving $@ to be processed by your loop. 2. Emulate "pass-by-reference" by converting your list argument into a single string, then splitting it back into an array within your function. -Tom -- Tom Metro Venture Logic, Newton, MA, USA "Enterprise solutions through open source." Professional Profile: http://tmetro.venturelogic.com/
- References:
- passing arrays and working with arrays in BASH
- From: tbs-Gb/NUjX2UK8 at public.gmane.org (theBlueSage)
- passing arrays and working with arrays in BASH
- Prev by Date: passing arrays and working with arrays in BASH
- Next by Date: Linux on netbooks
- Previous by thread: passing arrays and working with arrays in BASH
- Next by thread: Blocking a URL
- Index(es):