The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Creating an unknown number of arrays ajgwin Shell Programming and Scripting 2 05-23-2008 03:49 PM
Passing more than one argument in a function jisha Shell Programming and Scripting 1 04-23-2008 06:05 AM
Pass argument to function Knotty UNIX for Dummies Questions & Answers 1 04-08-2007 08:26 AM
creating email based on an output saswerks Shell Programming and Scripting 1 02-14-2005 06:57 PM
Passing Argument to Function AkumaTay UNIX for Dummies Questions & Answers 2 10-18-2001 06:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2005
nix21 nix21 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 5
bash 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[$i]=${line}
let i+=1
done
}

func "offsets"
_______________________________

So I was trying to name the array "offsets", but the way in which I have it just renames the array to whatever is in the first line of the file. I want to be able to call the function multiple times and set different array names.

adding a dollar sign to just reference the name doesn't work:
$arrayName[$i]=${line}

thanks for any help.

Last edited by nix21; 03-19-2005 at 03:30 PM..
  #2 (permalink)  
Old 03-20-2005
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
You probably can do this. I am not at a terminal now to play with any code but you can research this by looking at the man pages for eval and typeset , specifically, see the -A flag
  #3 (permalink)  
Old 03-20-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Try:
eval ${arrayName}[\$i]=\${line}
  #4 (permalink)  
Old 03-20-2005
nix21 nix21 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 5
Perderabo: I gave it a try but it just placed the name of the array (offsets) in the first postion and did not put anything else in the array.

google: I'll take a look at those. I tried eval a couple times, but nothing so far.

I did come up with a workaround. I just changed the original code I posted, by starting the variable 'i' at 1 instead of 0. This allows the name of the array to be placed at the 0 postion and the data from 1...n. I just have to be aware in the rest of the script that the data will always start at 1.

I would still like to find another way, without having to use the little workaround though.
  #5 (permalink)  
Old 03-20-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Just tried it...it works for me...
Code:
#! /usr/bin/ksh

func(){
#take in 1st arg as the arrayname
arrayName=$1

let i=0
while read line
do
eval ${arrayName}[\$i]=\${line}
let i+=1
done
}

func "offsets"

echo offsets = ${offsets[*]}
exit 0
$ ./ar
one
two
three
^Doffsets = one two three
$
  #6 (permalink)  
Old 03-20-2005
nix21 nix21 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 5
yeah your right it works. I have no idea what the problem was, I copied the specific line in and it didn't work with everything else the same. But then copied in the whole function and it worked fine. maybe a null character or something.

thanks, its much appreciated!
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 05:22 PM.


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