The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-02-2008
carlos25 carlos25 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 10
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 (permalink)  
Old 10-04-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
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 (permalink)  
Old 10-05-2008
carlos25 carlos25 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 10
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 (permalink)  
Old 10-05-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
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 (permalink)  
Old 10-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by carlos25 View Post
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]}"

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0