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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Real array in UNIX script

Status
Not open for further replies.

Disruptive

Programmer
Mar 8, 2005
40
GB
Hi

I am keen to create an array which stores real numbers which I can then loop through. I have tried using the bash script, but I cant see any literature which states whether I can actually get real numbers into the arrays. The sort of code I have put together is:

#!/bin/bash


KT[1]= 1.0

or I have tried...

KT[2]= "1.2"

Can I do this??

How can I get real numbers into the arrays.
 
What do you want to do in a shell script with real numbers ?
I think that awk is best suited for that.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I am using the script to run through a series of combinations which will be inserted into files and then run using awk for example. However I have a complicated range of numbers and have listed them in an array which I can loop through. I can think of a better or smarter way to do it, I just cant seem to get those numbers into the array elements.

Is there a way of getting the numbers in?
 
ksh93 handles real numbers wih typeset -E (or -F).
I don't know if bash does.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
typeset -A -E KT
KT[0]=1.1
KT[1]=1.2
echo $((KT[0]+KT[1]))
2.3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
There is no reason why you can't store real numbers in an array in ksh or bash provided you aren't planning to perform any calculations with them. The shell will just treat them as strings.

[tt]$ KT[1]=1.0
$ KT[2]=1.2
$ KT[3]=1.5
$ i=1
$ while ((i<=${#KT[*]})) ; do echo ${KT[$i]} ; ((i=i+1)) ; done
1.0
1.2
1.5
$[/tt]

Annihilannic.
 
Thats great!

Sorted out for the time being. Thanks Guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top