For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > er... help?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author er... help?
Gernot Frisch

2005-01-26, 3:55 pm

Hi,

I got this file:

#define A 1
#define B 2
#define C 2
#define D 1000

and it must become:






--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________

Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


Gernot Frisch

2005-01-26, 3:55 pm

sorry-the mail got sent incidentally:

I got this file:

#define A 1
#define B 2
#define C 2
#define D 1000

and it must become:

#define A 1
#define B 2
#define C 3 <- changed
#define D 1000

See, B and C must not have the same number, however D should stay
1000, not 4.


cat resource.h | gawk "{
if($1==\"#define\")
{
if((int)$2<=curnum)
{
curnum=curnum+1;
}
else
{
curnum=(int)$2;
}
printf(\"#define %-50s %6d\r\n\", $2, curnum);
}
else
{
printf(/"%s\r\n/", $0);
}
}"

however, the var: curnum seems to be reset to '0' every line, right?
Can you help?

-Gernot



William James

2005-01-26, 3:55 pm


Gernot Frisch wrote:
> I got this file:
>
> #define A 1
> #define B 2
> #define C 2
> #define D 1000
>
> and it must become:
>
> #define A 1
> #define B 2
> #define C 3 <- changed
> #define D 1000
>
> See, B and C must not have the same number, however D should stay
> 1000, not 4.



awk -f inc-def.awk resource.h

The file inc-def.awk contains (remove ": " at start of lines):

: /^#define/ {
: if ($3 <= curnum)
: curnum++
: else
: curnum = int( $3 )
: printf "#define %-50s %6d\n", $2, curnum
: next
: }
:
: { print }

Ed Morton

2005-01-26, 3:55 pm



Gernot Frisch wrote:

> sorry-the mail got sent incidentally:
>
> I got this file:
>
> #define A 1
> #define B 2
> #define C 2
> #define D 1000
>
> and it must become:
>
> #define A 1
> #define B 2
> #define C 3 <- changed
> #define D 1000
>
> See, B and C must not have the same number, however D should stay
> 1000, not 4.


Try this (untested):

awk '/#define/{if($3==prev)$3=$3+1;prev=$3}1' file

Regards,

Ed.

Gernot Frisch

2005-01-27, 8:55 am


"Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
news:ct8r0k$pqc@netnews.proxy.lucent.com...
>
>
> Gernot Frisch wrote:
>
>
> Try this (untested):
>
> awk '/#define/{if($3==prev)$3=$3+1;prev=$3}1' file


Nice, but that's not gonna work for:
A 1
B 1
C 2
Because, you get:
A 1
B 2
C 2 !!! which must be 3
Thanks anyway.
-Gernot


Chris F.A. Johnson

2005-01-27, 8:55 am

On Thu, 27 Jan 2005 at 08:34 GMT, Gernot Frisch wrote:
>
> "Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
> news:ct8r0k$pqc@netnews.proxy.lucent.com...
>
> Nice, but that's not gonna work for:
> A 1
> B 1
> C 2
> Because, you get:
> A 1
> B 2
> C 2 !!! which must be 3


Then you should define your requirements more precisely.

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Ed Morton

2005-01-27, 3:56 pm

Gernot Frisch wrote:
> "Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
> news:ct8r0k$pqc@netnews.proxy.lucent.com...
>
>
>
> Nice, but that's not gonna work for:
> A 1
> B 1
> C 2
> Because, you get:
> A 1
> B 2
> C 2 !!! which must be 3
> Thanks anyway.
> -Gernot


It's also won't work for a file containing:

I don't know how
to post a usenet question
showing my real requirements

where you want the last character to be an exclamation mark. Sorry for
my misunderstanding.

Ed.







Gernot Frisch

2005-01-27, 3:56 pm

> It's also won't work for a file containing:
>
> I don't know how
> to post a usenet question
> showing my real requirements
>
> where you want the last character to be an exclamation mark. Sorry
> for my misunderstanding.


echo I learned | gawk 'for(i=0; i<1000; ++i) {printf("I must specify
questions more precisely\n");}}'

-Gernot


Ed Morton

2005-01-27, 3:56 pm



Gernot Frisch wrote:
>
>
> echo I learned | gawk 'for(i=0; i<1000; ++i) {printf("I must specify
> questions more precisely\n");}}'


Now that's out the way... do you need any help figuring out a solution
for your real input files or have you got a working solution?

Ed.
Gernot Frisch

2005-01-27, 3:56 pm


"Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
news:ctb2vj$4hh@netnews.proxy.lucent.com...
>
>
> Gernot Frisch wrote:
>
> Now that's out the way... do you need any help figuring out a
> solution for your real input files or have you got a working
> solution?


Thank you, I've got it working already.
William James' version worked perfectly (with slight modification).
-Gernot


William James

2005-01-27, 3:56 pm


Gernot Frisch wrote:
> William James' version worked perfectly (with slight modification).
> -Gernot


Glad to heard that it works for you.

William James

2005-01-27, 3:56 pm

William James wrote
> Glad to heard that it works for you.


Make that:
Glad to hear that it works for you.

I didn't change your algorithm. If the code works correctly,
you deserve most of the credit.

Patrick TJ McPhee

2005-01-28, 3:55 am

In article <35pv49F4p9pt6U1@individual.net>,
Gernot Frisch <Me@Privacy.net> wrote:

A few comments about your script.

First, it's probably better to put the script in a file, or, assuming
some unix shell, to enclose it in single quotes. You've failed escape
all sorts of things which will be interpreted in a way you don't want.
I'm going to strip off the quotes and the escaping that you do have.


% {
% if($1=="#define")
% {
% if((int)$2<=curnum)

I suppose what you want this to do is coerce $2 into being
an int. Actually, what it does is evaluate the variable int
then concatenate its value with the value of $2, forcing $2
to be interpreted as a string. You should usually just write

% if($2<=curnum)

and if you really do need to force an expression to be like
a number, add 0 to it.

% if(($2+0)<=curnum)

% curnum=(int)$2;

The same goes here.

awk will automatically change strings to numbers at points where
it doesn't make sense not to, but for comparisons, it won't
necessarily. Anyway, removing (int) from both lines will fix
the problem.



--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Patrick TJ McPhee

2005-02-01, 3:56 pm

In article <35pv49F4p9pt6U1@individual.net>,
Gernot Frisch <Me@Privacy.net> wrote:

A few comments about your script.

First, it's probably better to put the script in a file, or, assuming
some unix shell, to enclose it in single quotes. You've failed escape
all sorts of things which will be interpreted in a way you don't want.
I'm going to strip off the quotes and the escaping that you do have.


% {
% if($1=="#define")
% {
% if((int)$2<=curnum)

I suppose what you want this to do is coerce $2 into being
an int. Actually, what it does is evaluate the variable int
then concatenate its value with the value of $2, forcing $2
to be interpreted as a string. You should usually just write

% if($2<=curnum)

and if you really do need to force an expression to be like
a number, add 0 to it.

% if(($2+0)<=curnum)

% curnum=(int)$2;

The same goes here.

awk will automatically change strings to numbers at points where
it doesn't make sense not to, but for comparisons, it won't
necessarily. Anyway, removing (int) from both lines will fix
the problem.



--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com