function return array | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

function return array

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-05-2008
dophine dophine is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 24 June 2010, 6:48 AM EDT
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
function return array

Hi all

I would like to know if there is a way to return an array for a function.
As I know function can return all the contents in an array, I want to return an array type.
Sponsored Links
    #2  
Old 05-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
 
Join Date: May 2008
Last Activity: 28 October 2009, 7:03 PM EDT
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
What scripting language?
Sponsored Links
    #3  
Old 05-05-2008
dophine dophine is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 24 June 2010, 6:48 AM EDT
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
bash shell script
    #4  
Old 05-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
 
Join Date: May 2008
Last Activity: 28 October 2009, 7:03 PM EDT
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
A function can only return a return code, which is a cardinal (positive integer or zero).

However a function can modify an array held in the calling script.


Code:
$ cat function_return_array
#!/bin/bash

function myarray()
{
        a[1]=moja
        a[3]=three
}

a[1]=one
a[2]=two

myarray

echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
$ ./function_return_array
moja
two
three
$

Sponsored Links
    #5  
Old 05-05-2008
dophine dophine is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 24 June 2010, 6:48 AM EDT
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
so the array seems act as a globle variable,right?
if a function is called several times in the script, then I need to store it in another array before calling it again, right?
Sponsored Links
    #6  
Old 05-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
 
Join Date: May 2008
Last Activity: 28 October 2009, 7:03 PM EDT
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
That's correct.
Sponsored Links
    #7  
Old 05-06-2008
dophine dophine is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 24 June 2010, 6:48 AM EDT
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
then I have another issue. how to assign an array value to another one i.e. clone?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Return a value from called function to the calling function mvictorvijayan Shell Programming and Scripting 1 09-14-2009 04:19 AM
return in function PRKS Shell Programming and Scripting 5 07-30-2009 08:15 AM
Return an array of strings from user defined function in awk user_prady Shell Programming and Scripting 2 12-03-2007 11:03 PM
Function to return an array of integer dwgi32 Programming 2 11-20-2007 01:08 AM
return value of a function prez Shell Programming and Scripting 3 08-22-2007 05:08 PM



All times are GMT -4. The time now is 04:49 AM.