|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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]"
}
}1but 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 errorI 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
|
||||
|
||||
|
Quote:
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Hi balajesuri !
ok, I see Last edited by lucasvs; 04-09-2012 at 12:59 AM.. |
|
#4
|
||||
|
||||
|
Hint: you are defining the function n times and it is in the wrong section.
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Hi Scrutinizer ! Quote:
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])
}
}
}1But 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
|
||||
|
||||
|
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
|
|||
|
|||
|
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)
}
}1output: Code:
AAAAAA|, , , , , |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|