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

Working With Multiple Arrays

Status
Not open for further replies.

FinnMan

Technical User
Feb 20, 2001
75
US
I have 3 arrays I have created from 3 different files. One file is a list of text and the other two are numeric in nature. I need to do a math equation all the way down the two numeric arrays (i.e., compare A[1] with B[1] and A[2] with B[2],etc..) and then dump the output to a file with the reference from the 3rd array.

in short:

A[1] Math Expression B[1]
echo result and place C[1] next to it in a new file.

Pointers?

Best Regards,
FM
 
Code:
i=0
while (( $i<${#A[*]} ))
 do

   sum = $(( A[$i] + B[$i] ))  
   echo "${sum} C[$i]" >> newFile

   i=$((${i}+1))

done

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Something like this ?
i=0;while [ $i -lt ${#C[@]} ]
do ((v=A+B)) # Your math expression here
echo "$v ${C[$i]}
((i+=1))
done >/path/to/newfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Ok..I've fought with this a while now. I'm starting to understand why they call it BASH as that's what I'm starting to do with my head on the desk.

Here is what I've got so far:

*********************************************************

#!/bin/bash
declare -a userHOLD
declare -a currentHOLD
declare -a quotaHOLD
userHOLD=hold1.$$
currentHOLD=hold2.$$
quotaHOLD=hold3.$$
awk -F "/" '{print $5}' usersample >> $userHOLD;
cat usersample | awk -F "/" '{print $1}' >> $currentHOLD;

for i in `cat $userHOLD`
do
ldapscript2 $i | grep Quota | awk '{print $2}' >> $quotaHOLD
done
set -x
j=0
while (( $j<${#currentHOLD[@]} ))
do
if [${currentHOLD[$j]} -gt ${quotaHOLD[$j]}]; then
echo "${userHOLD[$j]} is over quota!"
else
echo "${userHOLD[$j]} is not over quota!"

fi
j=$((${j}++1));
done>RESULTS
exit 0
*********************************************************

I threw in the set -x to see what was going on. It appears that the IF statement is getting interpreted as a LITERAL and not the value of the array. I.E., hold2.17966: command not found. Is there a syntax error in here that I'm not seeing?
 
put a space AFTER '[' and BEFORE ']' like so:

Code:
        if [ ${currentHOLD[$j]} -gt ${quotaHOLD[$j]} ]; then

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Well, at least I get a different error now!


limits.sh: line 17: (:) [ 0<hold2.17721 ] : syntax error: operand expected (error token is "[ 0<hold2.17721 ] ")


FM
 
isn't $quotaHOLD a file?
You can ONLY compare variables.
You'll have to rethink your implementation.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yes, $quotaHOLD is a file. I use the following to pipe a list of values into it (which that part works ok):

ldapscript2 $i | grep Quota | awk '{print $2}' >> $quotaHOLD

And I want to go straight down each of the values in $currentHold and compare it with $quotaHOLD. Logically, I'd think it should work as I'm comparing values in an array (or at least that's what I'm trying to do).
 
And I want to go straight down each of the values in $currentHold and compare it with $quotaHOLD. Logically, I'd think it should work as I'm comparing values in an array (or at least that's what I'm trying to do).

well, right now you're comparing values stored in the $currentHold array and the FILE $quotaHOLD

I am not sure if that's what you'd wanted to do.

vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,

Can you explain that to me? Why is $currentHold executing correctly as an array but not $quotaHOLD ?

Thx!
FM
 
what IS ${quotaHOLD}? File, right?
what IS ${currentHold}? Array, right?

Code:
 if [${currentHOLD[$j]} -gt ${quotaHOLD[$j]}]; then

What IS being compared here?
You have a value in the '$j'-th cell of the currentHOLD array
AND what're you comparing it TO?

The construct ${quotaHOLD[$j]} does not make any sense as the ${quotaHOLD} is NOT an array - it's a STRING variable containing a file name.

Do you see the confusion here?

vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yeah, I understand the "what" but I'm not getting the "why". Is there a way to recall hold.$$ back as an array?
 
ok

here's a quick hint:

given data file arr.txt
11
22
33
44
55
66
77
88

and a script arr.sh
Code:
#!/bin/ksh

file='arr.txt'

set -A fileA $(<${file})

typeset -i idx=0
while (( $idx < ${#fileA[@]} ))
do
    echo "idx->[${idx}] value->[${fileA[${idx}]}]"
    idx=$(( idx + 1 ))
done

I believe this scenario closely resembles your input.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top