Creating an unknown number of arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating an unknown number of arrays
# 1  
Old 05-23-2008
Creating an unknown number of arrays

I need to create arrays like this:

cnt=0
{ while read myline; do
if [[ $myline = "<meas>"* ]];then
firstpass="${myline##<meas>}"
meas="${firstpass%%</meas>}"
tempmeas="${meas%%;*}"
MEAS$cnt[$cnt]=$tempmeas
print $cnt
print ${MEAS'$cnt'[$cnt]}
line1=$line1$meas$colon$stream$colon
((cnt=cnt+1))
fi
done } < $dspbfile

What I want is MEAS0[0]="First Meas"
MEAS1[0]="Second Meas"
MEAS2[0]="Third Meas"

I will not know how many I need until I am in this loop. Can I do something like this? Right now I am getting these errors:
./PDSBatch.ksh[318]: MEAS$cnt[0]=MFC: not found
0
./PDSBatch.ksh[320]: ${MEAS"\$cnt"[$cnt]}: bad substitution

Is there a way to do this? Thanks.

Allyson
# 2  
Old 05-23-2008
I figured it out. I am going to do this:


mymeas=`echo "MEAS"$cnt`
echo $mymeas


and use $mymeas as each array.

Thanks.

Allyson
# 3  
Old 05-23-2008
Well, I thought I had it. I can fill up my arrays and if I specify them by whole name then I get the values. But if I try to use the variable to get the values, then I get an error. Here is where I am trying to get them out:

wholemeas=`echo "MEAS"$cnt`
echo $wholemeas
echo ${#MEAS0[*]}
**** ERROR eval $wholemeas[0]
echo ${MEAS0[*]}
echo ${MEAS0[0]}
echo ${MEAS0[1]}
echo ${MEAS0[2]}
echo ${MEAS0[3]}

All the lines produce the correct data except the one I mared with ERROR. I have tried all different combinations of eval, echo and without either and I can't seem to get the data that I want. I want MFC which is what the line echo ${MEAS0[0]} produces. wholemeas = MEAS0 in this case.

Does anyone know how to do this? Thanks.

Allyson
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating script with multiple job arrays

Hello everyone, First of all this is my first post and im fairly new to working with Unix and creating scripts etc. so there will probably be wrong phrases used. Lets get to my questions. I have multiple scripts that submit Slurms/Jobs to the cluster starting like this and doing certain... (3 Replies)
Discussion started by: idbemad
3 Replies

2. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

3. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

4. UNIX for Advanced & Expert Users

Unix script seems to be momentarily creating child process for unknown reason

Hi, I have a unix script that basically has a while loop inside which it checks Oracle database for certain records. If it finds the records, it does some processing and then goes back to the while loop. If it doesnot find any matching records, then it sleeps for 30 seconds and then goes back to... (17 Replies)
Discussion started by: waavman
17 Replies

5. Shell Programming and Scripting

grep unknown number of params

Hey i got trivial question but i cant figure it out. I want to grep a file for multiple unknown parameters. The user will enter these parameters at the command line. For example... ./program red apple filename This would grep for the phrase red apple. But i cant just do $1 $2 because the... (8 Replies)
Discussion started by: GmGeubt
8 Replies

6. Shell Programming and Scripting

Need help in creating arrays using shell

Hi, I need help in creating a array in shell scirpt. I have a file which has following details. hostname devices device1 device 2 de abcdmhs10 1234 2343 2353 3343 3435 2343 bcdfmhs11 2343 2443 3434 8874 0343 3434 (5 Replies)
Discussion started by: jpkumar10
5 Replies

7. Shell Programming and Scripting

Joining two arrays and then creating a variable

Hello all... I'd like to create a variable from an array element from two arrays. In my search for answers I found this code for bash that joins two arrays and then started to work with it. I had got to work once and then foolishly without saving the code, I started to edit it for ksh and... (4 Replies)
Discussion started by: carlos25
4 Replies

8. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

9. Shell Programming and Scripting

printf returning unknown number

Hi, Can anybody tell me why this function returns 49? printf "%d\n" \'10 I don't understand what the \ and ' are there for? thanks!!! (1 Reply)
Discussion started by: m0nk3y
1 Replies

10. Shell Programming and Scripting

shell: creating different arrays based on function argument

hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this: _____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName=${line} let i+=1... (5 Replies)
Discussion started by: nix21
5 Replies
Login or Register to Ask a Question