Return an array of strings from user defined function in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return an array of strings from user defined function in awk
# 1  
Old 12-03-2007
Data Return an array of strings from user defined function in awk

Hello Friends,

Is it possible to return an array from a user defined function in awk ?

example:
Code:
gawk '
BEGIN{}
{
   catch_line = my_function(i) 
   print catch_line[1]
   print catch_line[2]
   print catch_line[3]
}
function my_function(i)
{
 print "echo"
 line[1]= "awk"
 line[2]= "gawk"
 line[3]= "nawk"

 return line
}
' filename

Actually when I m returning a array from a function it shows me error.. Is it possible to return array ?

Pls help..

Thanks in advance..
# 2  
Old 12-03-2007
no need to return

Hi,
I am not very sure about your req. But actually there is no need to return. The variable in your user-defined function will be recognized by awk. You can just deal with any variable in function, and out of function in awk other parts you can refer to it.

Just modify your code to follow:

gawk '
BEGIN{}
{
my_function(i)
print line[1]
print line[2]
print line[3]
}
function my_function(i)
{
print "echo"
line[1]= "awk"
line[2]= "gawk"
line[3]= "nawk"
}' filename
# 3  
Old 12-04-2007
Quote:
Originally Posted by summer_cherry
Hi,
I am not very sure about your req. But actually there is no need to return. The variable in your user-defined function will be recognized by awk. You can just deal with any variable in function, and out of function in awk other parts you can refer to it.

Just modify your code to follow:

gawk '
BEGIN{}
{
my_function(i)
print line[1]
print line[2]
print line[3]
}
function my_function(i)
{
print "echo"
line[1]= "awk"
line[2]= "gawk"
line[3]= "nawk"
}' filename
Yes Genious .. You are absolutely right...That is my requirment...

Thanks summer_cherry........
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0. fun1() { t=$1 printf "%02d\n", $t % 60; } echo "Hi There 23" | awk '{print $0; system(fun1 $3)}' Any suggestions what to be modified in above code to achieve requirement.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

2. UNIX for Dummies Questions & Answers

Problem syntax with user-defined function

Hi ! I got a script from Arabic to Roman numeral conversion - .comp.lang.awk, that I would like to modify to apply it on my input file. input ("|"-delimited fields): AAAAAA|1, 10, 13, 14, 25, 60 wanted output: AAAAAA|I, X, XIII, XIV, XXV, LX script.awk: #!/usr/bin/gawk -f ... (11 Replies)
Discussion started by: lucasvs
11 Replies

3. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

4. Shell Programming and Scripting

How to pass parameter to User defined function in shell script?

Hello, Can anyone guide me tin passing parameters into user defined function of shell script (KSH). Here is my code, InsertRecord() { DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF set head off set feed off set serveroutput on INSERT INTO TBL1 ( OLD_VAL, NEW_VAL, ... (7 Replies)
Discussion started by: Poonamol
7 Replies

5. UNIX and Linux Applications

strange behavior of PSQL user defined function

Segregated the problematic portion, and showing for your view here., 1. Following is the function definition, create or replace function new_del(id integer) returns void as $$ begin raise info 'dollar :%',$1; delete from testing where id=$1; end ; $$ language 'plpgsql'; ... (1 Reply)
Discussion started by: thegeek
1 Replies

6. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

7. Shell Programming and Scripting

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. (6 Replies)
Discussion started by: dophine
6 Replies

8. Programming

Function to return an array of integer

Hi all, I am trying to create a function that return an array of integer based on the char parameter pass into the function. I.e. func_a(char * str) { example str is equal to "1,2,3,4" return an array of integers of 1,2,3,4 } Please advise regards dwgi32 (2 Replies)
Discussion started by: dwgi32
2 Replies

9. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

10. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies
Login or Register to Ask a Question