![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| joining variable to the end of a file name | naamas03 | UNIX for Dummies Questions & Answers | 7 | 08-04-2008 09:24 AM |
| Creating an unknown number of arrays | ajgwin | Shell Programming and Scripting | 2 | 05-23-2008 04:49 PM |
| creating a variable | rexmabry | UNIX for Dummies Questions & Answers | 3 | 02-22-2008 03:30 PM |
| shell: creating different arrays based on function argument | nix21 | Shell Programming and Scripting | 5 | 03-20-2005 11:34 PM |
| Can there be multi-dimensional variable arrays in borne shell? | jparker | Shell Programming and Scripting | 4 | 05-15-2002 05:47 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|