Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting of all array elements (ksh)

Status
Not open for further replies.

ns705

Programmer
Joined
Feb 4, 2004
Messages
29
Location
GB
Hi,

can you help me please?

Is it possible to store the same value in all array elements with a single command (in ksh script)?
I heard it's possible but I can't find it in my documentation.
Maybe it's only my dream...

Thanks in advance.

N.
 
To

Subsitute value of all elements use

${array[*]}

Other useful ones

${array} Substitute value of element i
$array Substitute value of first element (array[0])
${#array[*]} Substitute number of elements
array=val Store val into array

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Thank you, Mike.

Yes, I know it.
But it works for substitution.
I need something about:
all-array-elements=value
in order to store this value in all the array elements.
I have thought maybe it's possible to do via eval,
but I could not find a solution in such way too.
It seems my ksh doesn't allow such action.

Nata

 
There is no way to assign a value to all elements of an array with a single statement, except if the array size is fixe and known :
[tt]set -A array Value Value Value Value Value[/tt]


You can define a function for that purpose:
[tt]
# $1 = Array name (default ARRAY)
# $2 = Value (default array name)
# $3 = # array elements to set (default $1 array size)
InitArray() {
typeset array=${1:-ARRAY}
typeset value=${2:-$array}
eval typeset size=${3:-"\${#${array}[*]}"}
[ "$size" -eq 0 ] && size=1
while [ $size -gt 0 ]
do
(( size -= 1 ))
eval $array[$size]=$value
done
}

set -A MyArray a b c d e
InitArray MyArray z # set all elements of MyArray to 'z'
[/tt]


Jean Pierre.
 
Thank you, Jean Pierre.

I have written something about your variant.

I had strong suspicions it was not possible to do.
But one guy assured me that he was sure it could be done (although he didn't know in which way it could be done).

So I wasted my and your time in vain.

Thank you.

Nata
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top