Merging 2 Arrays in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging 2 Arrays in a script
# 1  
Old 12-10-2013
Merging 2 Arrays in a script

I am creating (with help) my own version of a calculator script that simply merges 2 arrays ( ${a[0]}${b[0]}${a[1]}${b[1]}... etc )

Code:
#!/bin/bash
echo "Enter the integers you would like to calculate"
read -a nums
echo "You entered ${#nums[@]} integers"
let batch="${#nums[@]}-1"
echo "Enter how you want to calculate these integers ($batch calculations)"
read -a calcs
result="$( paste <( printf "%s\n" ${nums[@]} ) <(printf "%s\n" ${calcs[@]}) )"
echo ${result[@]} =
echo ${result[@]} | bc

#Entering +, -, or / for calculations seem to work fine, but "*" causes problems. Is there any way I can use * so that it only multiplies the numbers?
# 2  
Old 12-10-2013
Code:
#!/bin/bash
echo "Enter the integers you would like to calculate"
read -a nums
echo "You entered ${#nums[@]} integers"
let batch="${#nums[@]}-1"
echo "Enter how you want to calculate these integers ($batch calculations)"
read -a calcs

result=''
for (( i=0; i<=$batch; i++ ))
do
    result=$(echo "$result ${nums[i]} ${calcs[i]}")
done

echo "${result} ="
echo "${result}" | bc

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-10-2013
If it's just integer arithmetic you require, there is no need for bc you could just use:

Code:
echo "$(( ${result} ))"

If you want floating point calculations, you should specify a scale eg:

Code:
echo "scale=2 ; ${result}" | bc

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging 2 Arrays

I am trying to create a script that combines 2 arrays: #!/bin/bash read -a unix #(a c e g) read -a test #(b d f) #now I want to merge ${unix} with ${test}, one after another such that the result would be: (abcdefg) #I've tried quite a few options and can't seem to make it work (5 Replies)
Discussion started by: pbmitch
5 Replies

2. Shell Programming and Scripting

Script for merging six files into one

Hi all Need little help from you my aim is like merging the output of 6 files (log_files) And merge it into the one master file tell me the script for this please thanks in advance Please reply in mail if possible <email removed> Regards: zimmy (1 Reply)
Discussion started by: zimmyyash
1 Replies

3. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

4. Shell Programming and Scripting

perl : merging two arrays on basis of common parameter

I have 2 arrays, @array1 contains records in the format 1|_|X|_|ssd|_| 4|_|H|_|hbd|_| 9|_|Y|_|u8gjdfg|_| @array2 contains records in the format X|_|asdf|_| Y|_|qwer|_| A|_|9kdkf|_| @array3 should contain records in the PLz X|_|ssd|_|asdf|_| Y|_|hdb|_|qwer|_| PLZ dont use... (2 Replies)
Discussion started by: centurion_13
2 Replies

5. Shell Programming and Scripting

Script for merging files

I am facing a strange situation where I need to collect logs and consolidate them. All logs are under say /tmp/useless and all logs are under subdir of the form IPaddresses_x_y eg: /tmp/useless/105.52.34.246_3_1 /tmp/useless/105.52.1.118_0_10 There are several logs under each... (2 Replies)
Discussion started by: zpn
2 Replies

6. Shell Programming and Scripting

Shell script for 2 arrays

I have 2 arrays abc and xyz abc = ( a b c d e f g h i j k l m n o p q r s t u v w x y z ) and xyz = ( b c d e f ) lets assume a .... z are the file name. I have to perform a pattern replacement on each file present in abc array accept the files i have in xyz array. and i am doing... (4 Replies)
Discussion started by: kukretiabhi13
4 Replies

7. UNIX for Dummies Questions & Answers

writing script using arrays

Hi, I want to write a script that is designed to find the largest entry in a nonempty list of integers. i was thinking about using array variables, but i'm not sure any ideas? (3 Replies)
Discussion started by: FOBoy
3 Replies

8. Shell Programming and Scripting

Merging arrays

Hi all, I need some help in merging arrays. I have two arrays and using korn shell Array1 AB23 AB24 Array2 CD00 CD01 CD02 Elements from array 1 should always alternate with elements of arrays 2 i.e the result should look like AB23CD00 AB24CD01 AB23CD02 Any help is appreciated.... (4 Replies)
Discussion started by: jakSun8
4 Replies

9. Shell Programming and Scripting

arrays in shell script

Hi, can we use arrays in shell script? I have this so far: #!/bin/sh isql -Usa -P -S DBSERVER -o output << EOF sp_transactions "state", "prepared", "xactname" go / EOF the file 'output'' will be have many entries and I want to caputre the transaction names only: grep 000 output works... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. Shell Programming and Scripting

ksh script - arrays

hello all.. I have been browsing / searching through the forum and have yet not been able to find what I was looking for. I am fairly new to ksh and my task is to create a function that reads in an input file: ***************** 2 3 1 abc def ghi /dev/sid/ *****************... (13 Replies)
Discussion started by: sidamin810
13 Replies
Login or Register to Ask a Question