Joining two arrays and then creating a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining two arrays and then creating a variable
# 1  
Old 10-02-2008
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 subsequently broke it. Now I can't get it to work again. Any suggestions?

BASH CODE >>>

#!/bin/bash

NAME=([0]="John" [1]="Mary" [2]="Fred")
SURNAME=([0]="Doe" [1]="Poppins" [2]="Flintstone")

# this gets args from command line and add them to the arrays.
# it's NOT the Right Thing, but can give you a brief idea of what to do
# You may add options, like -name "list" -surname "list" to parse the
# correct elements and insert them in the appropriated array.
for i in $@
do
# this ugly stuff inside brackets counts the number of elements
# so every new element added increases array counter.
NAME[${#NAME[@]}]=$i
SURNAME[${#SURNAME[@]}]=$i
done

# starts loop at element 0 and stops at last.
for ((i=0; i < ${#NAME[@]}; i++))
do
# echo element number, name and surname.
echo "Howdy #$i: ${NAME[$i]} ${SURNAME[$i]}"
done

<<<

What I thought I had come up with was:

MY VERSION OF THE BASH CODE >>>

NAME=("John" "Mary" "Fred")
SURNAME=("Doe" "Poppins" "Flintstone")

for i in $@
do

NAME[${#NAME[@]}]=$i
SURNAME[${#SURNAME[@]}]=$i
done

for ((i=0; i < ${#NAME[@]}; i++))
do

echo "Howdy #$i: ${NAME[$i]} ${SURNAME[$i]}"
NAME[$i]=${SURNAME[$i]}
done
echo "$John"
echo "$Mary"
echo "$Fred"

<<<

---
Carl
# 2  
Old 10-04-2008
What do you want it to do? What is not working?

As far as I can see, both scripts work in both bash and ksh93.
# 3  
Old 10-05-2008
Basically what I want to do is take SURNAME and assign it to NAME as a variable for each element.
This is what I have come up with... ( adding/changing - [ typeset NAME[$i]="${SURNAME[$i]}" ])
Code:
#!/bin/bash

NAME="John" "Mary" "Fred"
SURNAME="Doe" "Poppins" "Flintstone"

for i in $@
do

NAME[${#NAME[@]}]=$i
SURNAME[${#SURNAME[@]}]=$i
done

for ((i=0; i < ${#NAME[@]}; i++))
do

typeset NAME[$i]="${SURNAME[$i]}"

done

echo "$John"
echo "$Mary"
echo "$Fred"

As I mentioned in my original post I got it to work another way and couldn't replicate it again. I was just wondering if it was a fluke or another way of assigning an array I was not aware of.

A follow-up question to this is how I would approach combining the two arrays together into another single array?

example: display_name="NAME[$i] ${SURNAME[$i]}"

So that it would echo ${display_name[0]} and return "John Doe"

---
Carl
# 4  
Old 10-05-2008
An example:

Code:
#!/bin/bash

NAME=( "John" "Mary" "Fred" )
SURNAME=( "Doe" "Poppins" "Flintstone" )

n=0

while [ $n -lt ${#NAME[@]} ] ; do
  DISPLAY_NAME[$n]=${NAME[$n]}" "${SURNAME[$n]}
  echo ${DISPLAY_NAME[$n]}
  let n=n+1
done

With a for loop:

Code:
#!/bin/bash

NAME=( "John" "Mary" "Fred" )
SURNAME=( "Doe" "Poppins" "Flintstone" )

n=${#NAME[@]}

for (( i=0; i<n; i++ )); do
  DISPLAY_NAME[$i]=${NAME[$i]}" "${SURNAME[$i]}
  echo ${DISPLAY_NAME[$i]}
done

Regards

Last edited by Franklin52; 10-05-2008 at 02:04 PM.. Reason: adding example with for loop
# 5  
Old 10-05-2008
Quote:
Originally Posted by carlos25
Basically what I want to do is take SURNAME and assign it to NAME as a variable for each element.
This is what I have come up with... ( adding/changing - [ typeset NAME[$i]="${SURNAME[$i]}" ])
Code:
#!/bin/bash

NAME="John" "Mary" "Fred"
SURNAME="Doe" "Poppins" "Flintstone"

for i in $@


You should quote $@.
Quote:
Code:
do

NAME[${#NAME[@]}]=$i
SURNAME[${#SURNAME[@]}]=$i
done

for ((i=0; i < ${#NAME[@]}; i++))


That is non-standard syntax. To loop through all elements of an array:

Code:
for i in "${NAME[@]}"

Quote:
Code:
do

typeset NAME[$i]="${SURNAME[$i]}"


What's the point of typeset? It is non-standard, and doesn't do anything here.
Quote:
Code:
done

echo "$John"
echo "$Mary"
echo "$Fred"


Use eval:

Code:
eval "${NAME[$i]}=\${SURNAME[$i]}"

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

Variable substitution with arrays

Hi all, I have a script with the following gist: declare -a index=(0 1 2 3 4); declare -a animals=(dog cat horse penguin cow); declare -a fruits=(orange apple grapes peach mango); declare -a drinks=(juice milk coffee tea coke); declare -a cities=(toronto paris london glasgow... (18 Replies)
Discussion started by: Kingzy
18 Replies

3. Shell Programming and Scripting

Multiple arrays in variable using for loop

Hi, I'm trying to get the number of files inside same kind of folders on each disks and assigning each values in to a variable named with same folder and disk name so that it'll be easy for me to identify each time.But somehow I'm not able to assign those values in that specific name variable... (1 Reply)
Discussion started by: ratheeshp
1 Replies

4. Shell Programming and Scripting

Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this: I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called... (4 Replies)
Discussion started by: lily-anne
4 Replies

5. Shell Programming and Scripting

awk arrays - compare value in second column to variable

Hello, I am trying to redirect files to a directory by using a config file. The config files is as such: xxxxxx,ID,PathToDirectory xxxxxx,ID2,PathToDirectory2 and so on... I have a variable that should match one of these IDs. I want to load this config file into an awk array, and... (2 Replies)
Discussion started by: jrfiol
2 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

variable joining

I really need help on this problem. The story: My VAR1 and VAR2 works fine and able to get the value. I want to do as "if VAR1 or VAR2 is bigger than max_loadavg then the code will run...then if VAR1 or VAR2 is lesser than min_loadavg then the other code will run... The problem: The... (8 Replies)
Discussion started by: hezry79
8 Replies

8. UNIX for Dummies Questions & Answers

joining variable to the end of a file name

hi all i have a directory which contain file 20060101-66666-09-08-0.tif 20060101-77777-11-12-0.tif 20051231-54221-66-55.tif 20051231-54221-66-44.tif as you can see the name of the two last files is shorter then the first ones i want to take all the files with the shorter name and to add to... (7 Replies)
Discussion started by: naamas03
7 Replies

9. Shell Programming and Scripting

Creating an unknown number of arrays

I need to create arrays like this: cnt=0 { while read myline; do if ];then firstpass="${myline##<meas>}" meas="${firstpass%%</meas>}" tempmeas="${meas%%;*}" MEAS$cnt=$tempmeas print $cnt print ${MEAS'$cnt'} ... (2 Replies)
Discussion started by: ajgwin
2 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