[SOLVED] Arithmetic operation between 2 Arrays to generate a new Array
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Arithmetic operation between 2 Arrays to generate a new Array
Hi, I have defined to different arrays, ARRAY1 and ARRAY2. Each of them has 20 elements (integer numbers).
Code:
#!/bin/bash
ARRAY1=(0 3 7 3 9 10 6 8 8 8 7 3 8 4 4 7 1 6 7 6)
ARRAY2=(1 4 4 2 1 5 4 6 1 3 5 5 3 6 1 1 6 1 2 2)
for ((i; i < 20; i++))
do
if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
then
ARRAY3+=(${ARRAY2[i]})
else
ARRAY3+=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))
#Double brackets in each side to do with arithmetic operation
fi
done
echo ${ARRAY3[@]}
I have to compare each element in the position "i" in ARRAY1 to the element in position "i-1" in ARRAY2. If the element in ARRAY1 is either equal or greater than the element in ARRAY2, this element in ARRAY2 is added to a new ARRAY3. Otherwise I add to ARRAY 3 (element i-1 in ARRAY2 - element i in Array1 + element i in ARRAY3)
As an example.
ARRAY1[1]=3
ARRAY2[1-1]=1
ARRAY2[1]=4
Then element in ARRAY3 = ARRAY2[1] = 4
If if only test one set of elements,
Code:
if [ ${ARRAY1[12]} -ge ${ARRAY2[11]} ]
then
ARRAY3+=(${ARRAY2[12]})
else
ARRAY3+=$(( (${ARRAY2[11]}) - (${ARRAY1[12]}) + (${ARRAY2[12]}) ))
fi
the code function as expected, because it gives 3 as solution.
But when put the code inside of the for loop it doesn't work.
I think that the problem lays on how I put [i-1] inside the if clause.
Another doubt that I have is how is going to behave the code in the first iteration, because there is no [i-1] in first element of ARRAY2.
I hope you can shade some light in this issue.
Thanks!!
Why are you calling 3 an array? The way you're using it in your code it's just a number.
Anyway, you're not initializing "i" to anything, so of course it's not working. You'd probably want to start it at 1, since there is no "-1" element in array2.
Last edited by suicidaleggroll; 10-17-2013 at 04:59 PM.
Why are you calling 3 an array? The way you're using it in your code it's just a number.
Anyway, you're not initializing "i" to anything, so of course it's not working. You'd probably want to start it at 1, since there is no "-1" element in array2.
3 is the result of the example iteration. In that case it acts as a simple variable, but it is just and example.
What I need is to generate an array, that's way I put the code inside a for loop.
suicidaleggroll, can you explain to me how do I correctly initialize "i"?
Thanks!
3 is the result of the example iteration. In that case it acts as a simple variable, but it is just and example.
What I need is to generate an array, that's way I put the code inside a for loop.
Yet you're still treating "ARRAY3" as a scalar. Where's the array indexing that lets the program know in which index it needs to place the result of your calculation?
Quote:
Originally Posted by astalavista2000
suicidaleggroll, can you explain to me how do I correctly initialize "i"?
The way you're using the for loop in your code is literally no different than the way you do a for loop in any other language. There are three things you need to supply to any for loop:
1) Where to start
2) Where to stop
3) How to increment
You have #2 and #3 down, but not #1. In other words, you've told it to stop when i>=20, and to increment by 1 (i++), but you never told it where to start.
Last edited by suicidaleggroll; 10-17-2013 at 08:14 PM.
Yet you're still treating "ARRAY3" as a scalar. Where's the array indexing that lets the program know in which index it needs to place the result of your calculation?
The way you're using the for loop in your code is literally no different than the way you do a for loop in any other language. There are three things you need to supply to any for loop:
1) Where to start
2) Where to stop
3) How to increment
You have #2 and #3 down, but not #1. In other words, you've told it to stop when i>=20, and to increment by 1 (i++), but you never told it where to start.
OK. Now I understand what you are saying. I thought that there is no need to specify the index because it puts the result of the calculation in the "i" position determined by the for loop. And each time it iterates, there is a new available position.
Can you explain to me how do I treat ARRAY3 as an array, and how do I correct this line?
I really appreciate the time that you're taking to help me!
This is how I corrected the for loop. The code is almost ready.
Code:
for ((i=0; i<=20; i++))
do
if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
then
ARRAY3+=(${ARRAY2[i]})
else
ARRAY3+=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))
fi
done
PD. If I put i=1 instead of i=0 in the for loop I will avoid the situation that I described in which there is no i-1, won't I?
Last edited by astalavista2000; 10-17-2013 at 09:15 PM.
Reason: corrections
You simply need to assign the value to the correct index, and remove the "+=" since you're not incrementing anything, you're assigning:
Code:
ARRAY3[$i]=${ARRAY2[$i]}
etc.
I'm almost there.
Code:
for ((i=1; i<=20; i++))
do
[LINE 22] if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
then
Vector[$i]=${ARRAY2[i]}
else
[LINE 26] Vector[$i]=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))
fi
done
#I add the first position, where i-1 doesn't exist
ARRAY3[0]=${ARRAY2[0]}
echo ${ARRAY3[*]}
This is works as expected but with some errors. I got the following result
As you can see the ARRAY3 is showing the correct 20 numbers that are generated after the operations between ARRAY1 and ARRAY2.
I just need to correct the errors. Can you help me with that?
Thanks!
Grail, all the values of ARRAY1 and ARRAY2 are at the begining of the code.
Here is what you've asked:
ARRAY1[20]=6
ARRAY2[20]=2
ARRAY2[20-1]=2
if [ARRAY1[20] >= ARRAY2[20-1]]
Then
ARRAY3[20]=ARRAY2[20] that is "2"
You can see that the result of this operation is the same that the last element of ARRAY3 (I wrote the output in my previous post.
Thanks
SOLVED!
I debug the code and re-execute the script.
The Error was this: "./comparaVec.sh: line 22: [: -ge: unary operator expected"
And the last iteration in the debug mode showed this: + '[' -ge 2 ']'
-ge is a binary operator and I need two arguments. But, when i=20 ARRAY1 is empty! So I simple replace the i<=20 with i<20.
Thanks to both of you, suicidaleggroll for your help!
When ‘+=’ is applied to an array variable using compound assignment (see Arrays), the variable’s value is not unset (as it is when using ‘=’), and new values are appended to the array beginning at one greater than the array’s maximum index (for indexed arrays), or added as additional key-value pairs in an associative array.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.