Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-08-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
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-08-2012 at 11:43 PM.. Reason: .
Sponsored Links
    #2  
Old 04-08-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts
Quote:
Originally Posted by lucasvs View Post
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???
Sponsored Links
    #3  
Old 04-08-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
Hi balajesuri !

ok, I see

Last edited by lucasvs; 04-09-2012 at 12:59 AM..
    #4  
Old 04-09-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,341
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Hint: you are defining the function n times and it is in the wrong section.
Sponsored Links
    #5  
Old 04-09-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
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 01:00 AM..
Sponsored Links
    #6  
Old 04-09-2012
Peasant's Avatar
Registered User
 
Join Date: Mar 2011
Posts: 509
Thanks: 14
Thanked 104 Times in 102 Posts
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..
}

Sponsored Links
    #7  
Old 04-09-2012
Registered User
 
Join Date: Dec 2011
Posts: 84
Thanks: 29
Thanked 0 Times in 0 Posts
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|, , , , ,

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
How to pass parameter to User defined function in shell script? Poonamol Shell Programming and Scripting 7 10-13-2010 12:55 AM
strange behavior of PSQL user defined function thegeek UNIX and Linux Applications 1 04-28-2009 10:01 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
need help with User Defined Function user_prady Shell Programming and Scripting 11 11-18-2007 07:51 PM
Nawk user-defined function NewbieGirl Shell Programming and Scripting 1 06-20-2003 10:51 AM



All times are GMT -4. The time now is 05:26 PM.