Merging arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging arrays
# 1  
Old 07-01-2008
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.
JAK
# 2  
Old 07-01-2008
Code:
$ cat merge
#! /usr/bin/ksh

set -A Array1 AB23 AB24
set -A Array2 DC00 CD01 CD02
count=0
i1=0
i2=0
while (( count < ${#Array1[*]} || count < ${#Array2[*]})) ; do
        echo ${Array1[i1]}${Array2[i2]}
        ((count=count+1))
        ((i1=(i1+1)%${#Array1[*]}))
        ((i2=(i2+1)%${#Array2[*]}))
done
exit 0
$ ./merge
AB23DC00
AB24CD01
AB23CD02
$

# 3  
Old 07-01-2008
Thanks a bunch. I will give it a try

I have another question

i defined an array which is suppose to hold arrays
set -A pttrnArrays

I am also putting arrays in this array
something like
pttrnArrays[ctr]=${pttrnArray[*]}

I want to now retreive an array from pttrnArrays

Can i do something like
set -A Array1
Array1=${pttrnArrays[0]}

i tried this and it does not seem to work

Please let me know
# 4  
Old 07-01-2008
data="AB23 AB24"
set -A Array1 $data

seems to work. Maybe you can use something like that.

Array1=${pttrnArrays[0]}

is just missing an index. It is supposed to work just like

Array1[0]=${pttrnArrays[0]}
# 5  
Old 07-02-2008
Thanks, i tried set -A Array1 $(print ${pttrnArrays[0]}) and it worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on merging

I have a total of 100 files (variable size of each file) with total size of 328950 bytes. I want to merge those 100 files into 4 files with each size be close to equal size i.e (328950/4 ~= 82238) but do not want to break any file. Any unix sheel script help will be really helpful. (18 Replies)
Discussion started by: George1234
18 Replies

2. Shell Programming and Scripting

Merging 2 Arrays in a script

I am creating (with help) my own version of a calculator script that simply merges 2 arrays ( ${a}${b}${a}${b}... etc ) #!/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... (2 Replies)
Discussion started by: pbmitch
2 Replies

3. 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

4. Shell Programming and Scripting

Merging

Hi, I have searched the forums for a solution but I haven't found a perfect answer, and I'm a bit of a novice, so I hope someone can help: I have 2 files: file1: Chr1 139311 1/1:37,3,0:19 Chr1 139350 1/1:67,6,0:19 Chr1 139404 1/1:0,0,0:7 Chr1 152655 0/1:0,0,0:3 Chr1 152718... (2 Replies)
Discussion started by: ljk
2 Replies

5. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

6. 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

7. 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

8. Shell Programming and Scripting

merging

Hi all, I have 2 files. I want to merge a portion or column in file 2 into file 1. file 1 - not tab or space delimited B_1 gihgjfhdj| hgfkddlldjljldjlddl B_2 gihgjddshjgfhs| hgfkddlldjljldjlddl B_3 gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj file2 -... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Merging Help

Hi Gurus, I need a help in merging the files. I have nearly 7 files and the files will have time stamp in it. I need to merger these files condition is it is not necessary that all the 7 files has to be there. suppose if i have only 3 files availabe out of these 7 then i need to merge... (3 Replies)
Discussion started by: kumarc
3 Replies
Login or Register to Ask a Question