Problem syntax with user-defined function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem syntax with user-defined function
# 1  
Old 04-08-2012
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):
Code:
AAAAAA|1, 10, 13, 14, 25, 60

wanted output:
Code:
AAAAAA|I, X, XIII, XIV, XXV, LX

script.awk:
Code:
#!/usr/bin/gawk -f

BEGIN{FS=OFS="|"}

   n = split($2,a,", ")
        
   for (i=1; i<=n; i++){

        function dec2roman(n, v,w,x,y,r1,r10,r100,r1000)
        if (a[i] < 3999) {

        split("I II III IV V VI VII VIII IX",r1," ")
        split("X XX XXX XL L LX LXX LXXX XC",r10," ")
        split("C CC CCC CD D DC DCC DCCC CM",r100," ")
        split("M MM MMM",r1000," ")

        v = (a[i] - (a[i] % 1000)) / 1000
        a[i] = a[i] % 1000
        w = (a[i] - (a[i] % 100)) / 100
        n = a[i] % 100
        x = (a[i] - (a[i] % 10)) / 10
        y = a[i] % 10

        a[i] = "r1000[v] r100[w] r10[x] r1[y]"
        }

}1

but I got syntax error:
Code:
gawk: script.awk:7:     function dec2roman(n, v,w,x,y,r1,r10,r100,r1000) {
gawk: script.awk:7:     ^ syntax error

I tried to change position of the brackets, but still the same.

Last edited by lucasvs; 04-09-2012 at 12:43 AM.. Reason: .
# 2  
Old 04-09-2012
Quote:
Originally Posted by lucasvs
script.awk:
Code:
#!/usr/bin/gawk -f

BEGIN{FS=OFS="|"}

   n = split($2,a,", ")
        
   for (i=1; i<=n; i++){

        function dec2roman(n, v,w,x,y,r1,r10,r100,r1000)
        if (a[i] < 3999) {

        split("I II III IV V VI VII VIII IX",r1," ")
        split("X XX XXX XL L LX LXX LXXX XC",r10," ")
        split("C CC CCC CD D DC DCC DCCC CM",r100," ")
        split("M MM MMM",r1000," ")

        v = (a[i] - (a[i] % 1000)) / 1000
        a[i] = a[i] % 1000
        w = (a[i] - (a[i] % 100)) / 100
        n = a[i] % 100
        x = (a[i] - (a[i] % 10)) / 10
        y = a[i] % 10

        a[i] = "r1000[v] r100[w] r10[x] r1[y]"
        }

}1

but I got syntax error:
Code:
gawk: script.awk:7:     function dec2roman(n, v,w,x,y,r1,r10,r100,r1000) {
gawk: script.awk:7:     ^ syntax error

I tried to change position of the brackets, but still the same.
Where is "function dec2roman" defined???
# 3  
Old 04-09-2012
Hi balajesuri !

ok, I see

Last edited by lucasvs; 04-09-2012 at 01:59 AM..
# 4  
Old 04-09-2012
Hint: you are defining the function n times and it is in the wrong section.
# 5  
Old 04-09-2012
Hi Scrutinizer !

Quote:
Hint: you are defining the function n times and it is in the wrong section.
So I tried:
Code:
#!/usr/bin/gawk -f

BEGIN{FS=OFS="|"}
      
   n = split($2,a,", ")
       for (i=1; i<=n; i++){
           
           if (a[i] < 3999) { 
               function dec2roman(a[i],v,w,x,y,r1,r10,r100,r1000){
               	
               a[i] = int(a[i])
               
               split("I II III IV V VI VII VIII IX",r1," ")
               split("X XX XXX XL L LX LXX LXXX XC",r10," ")
               split("C CC CCC CD D DC DCC DCCC CM",r100," ")
               split("M MM MMM",r1000," ")

               v = (a[i] - (a[i] % 1000)) / 1000
               a[i] = a[i] % 1000
               w = (a[i] - (a[i] % 100)) / 100
               a[i] = a[i] % 100
               x = (a[i] - (a[i] % 10)) / 10
               y = a[i] % 10

               return(r1000[v] r100[w] r10[x] r1[y])
        }
   }
}1

But I got more syntax errors in the for loop and the function. And "unexpected newline or end of string" for everything else !

---------- Post updated at 11:51 PM ---------- Previous update was at 11:21 PM ----------

Solution:
I removed the function and use only its code !

Last edited by lucasvs; 04-09-2012 at 02:00 AM..
# 6  
Old 04-09-2012
Good practice would be i guess define functions first then other stuff.

Code:
function name ( val .. ) {
## code val ..
}

BEGIN {
## what you want to do before parsing the file(s)
## You can also use this section for reading files with getline or asking user to input variables
}

## action block  - body 
{
## code for input files goes here
## Here we shall process the input file(s)
}

END {
# what you want to do after the code up has ran
## Itterate over your arrays, print various sums etc.
}



You are have never opened action block and the function definition should be outside.
Code:
BEGIN{FS=OFS="|"}
{
## your code..
}

# 7  
Old 04-09-2012
Hi Peasant !

Thanks for your advices.

However when I use only the codes of the function it prints the original line in record 1, and then the wanted line in record2 !!!!

And when I use the function following your advice as below, it removes the numbers:
Code:
#!/usr/bin/gawk -f


function convert (n,v,w,x,y,r1,r10,r100,r1000){
	       split("I II III IV V VI VII VIII IX",r1," ")
               split("X XX XXX XL L LX LXX LXXX XC",r10," ")
               split("C CC CCC CD D DC DCC DCCC CM",r100," ")
               split("M MM MMM",r1000," ")
      
               v = (n - (n % 1000)) / 1000
             
               n = n % 1000
               w = (n - (n % 100)) / 100
               
               n = n % 100
               x = (n - (n % 10)) / 10
             
               y = n % 10
               
               }

BEGIN{FS=OFS="|"}
     
{b = split($2,a,", ")
       for (i=1; i<=b; i++){
       n = a[i]  
       sub (n,(r1000[v] r100[w] r10[x] r1[y]),$2)
       }
}1

output:
Code:
AAAAAA|, , , , ,

 
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

user defined commands

Hi, i would like to create user defined commands. e,g: if an user executes , mkdircd test then a directory called test should be created and it should be cd to test. How i can create the command mkdircd with below action: mkdir $1 && cd $1. Please help me in achieving this (7 Replies)
Discussion started by: pandeesh
7 Replies

3. 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

4. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

5. Shell Programming and Scripting

Clearing a defined function from memory

I guess what I mean is: is there something like unalias to clear functions defined by sourcing a script or config/alias file from memory? Is it necessary to start a new terminal session, or can it be done with a command? I tried "function somefunc {}" but bash told me it hit an "unexpected... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

6. 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

7. Shell Programming and Scripting

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: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

8. 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

9. UNIX for Dummies Questions & Answers

User defined service

I want to add a new IP service which executes a script on SCO OS5. I have amended /etc/services and added to port number (3333) I have amended /etc/inetd.conf and added a line for this service but I can't get it to execute my own shell script When I telnet to the IP address on port 3333 I... (1 Reply)
Discussion started by: markdrury
1 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