Rookie AWK Question...... 
Author Message
 Rookie AWK Question......

Hey all,

Here is a little snip of the code I'm playing with.....

{
for (i = 1; i <= NF; ++i) {
        if (comment == 0) {
               if ($i == "//") break;
               if ($i == "/*") {
                        comment = 1;
                        break;
                }
        } else
                if ($i == "*/") comment = 0;
        }

Quote:
}

There is some more to the code, but this is the important parts...
The problem? If I input a text file, with these two lines
// foo
//bar

notice the space after foo.
Well, the code pasted above will break on the // foo. However, it totally
ignores //bar. What modification do I have to make so that it will recognize
//bar?

Thanks for your help,
Xanatos



Sun, 10 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......
Why You don't try with:
substr($0,1,2) -> this give you first two characters from entire record ( //, /*
or other)
Quote:

> Hey all,

> Here is a little snip of the code I'm playing with.....

> {
> for (i = 1; i <= NF; ++i) {
>         if (comment == 0) {
>                if ($i == "//") break;
>                if ($i == "/*") {
>                         comment = 1;
>                         break;
>                 }
>         } else
>                 if ($i == "*/") comment = 0;
>         }
> }

> There is some more to the code, but this is the important parts...
> The problem? If I input a text file, with these two lines
> // foo
> //bar

> notice the space after foo.
> Well, the code pasted above will break on the // foo. However, it totally
> ignores //bar. What modification do I have to make so that it will recognize
> //bar?

> Thanks for your help,
> Xanatos



Sun, 10 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......

Quote:

>Hey all,

>Here is a little snip of the code I'm playing with.....

>{
>for (i = 1; i <= NF; ++i) {
>        if (comment == 0) {
>               if ($i == "//") break;
>               if ($i == "/*") {
>                        comment = 1;
>                        break;
>                }
>        } else
>                if ($i == "*/") comment = 0;
>        }
>}

>There is some more to the code, but this is the important parts...
>The problem? If I input a text file, with these two lines
>// foo
>//bar

>notice the space after foo.
>Well, the code pasted above will break on the // foo. However, it totally
>ignores //bar. What modification do I have to make so that it will recognize
>//bar?

Consider changing the:

               if ($i == "//") break;

to:

               if ($i ~ /^\/\// ) break;

or perhaps:

               if ($i ~ "^//") break;

I think the ^ for the beginning of record/field may be necessary
to stop getting triggered by things like http://www.domain.com

Cood luck on your comment processing, but it's tricky, especially
if the start and or finish of your comments can appear in quoted
text, i.e. in code like this:

printf("Comment strings may start with // on a line");

Dastardly stuff!  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 11 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......

[...]

% for (i = 1; i <= NF; ++i) {
%         if (comment == 0) {
%                if ($i == "//") break;

[...]

% There is some more to the code, but this is the important parts...
% The problem? If I input a text file, with these two lines
% // foo
% //bar

There's more than that. What if you have
 "/* is a comment start"

You pretty much have to go through the input a character at a time
in order to parse free-form text.

--

Patrick TJ McPhee
East York  Canada



Mon, 11 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......

Quote:
> Consider changing the:

>                if ($i == "//") break;

> to:

>                if ($i ~ /^\/\// ) break;

> or perhaps:

>                if ($i ~ "^//") break;

Hi Chuck,

I changed the code to if ($i ~ /^\/\// ) break; and much to my delight it
seems to be working "better"....

Quote:
> Cood luck on your comment processing, but it's tricky, especially
> if the start and or finish of your comments can appear in quoted
> text, i.e. in code like this:

> printf("Comment strings may start with // on a line");

You're telling me!
Of course, that is the example where it still falls apart.....but at least
I'm off to a much better start now :)

Cheers!
Xanatos

Quote:
> Chuck Demas
> Needham, Mass.



Mon, 11 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......

<snip>

Quote:

>> Cood luck on your comment processing, but it's tricky, especially
>> if the start and or finish of your comments can appear in quoted
>> text, i.e. in code like this:

>> printf("Comment strings may start with // on a line");

>You're telling me!
>Of course, that is the example where it still falls apart.....but at least
>I'm off to a much better start now :)

<snip>>

The way I've solved this type of problem in the past is to copy $0 and then
delete quoted strings from the copy using a suitable regex and gsub(), then
you can identify whether the // is inside quotes or not.

Peter
--


Opinions expressed are my own and not necessarily those
of my employer



Sun, 17 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......


Quote:


><snip>

>>> Cood luck on your comment processing, but it's tricky, especially
>>> if the start and or finish of your comments can appear in quoted
>>> text, i.e. in code like this:

>>> printf("Comment strings may start with // on a line");

>>You're telling me!
>>Of course, that is the example where it still falls apart.....but at least
>>I'm off to a much better start now :)

><snip>>

>The way I've solved this type of problem in the past is to copy $0 and then
>delete quoted strings from the copy using a suitable regex and gsub(), then
>you can identify whether the // is inside quotes or not.

While that will work for many cases, it does not work when the starting
and ending quotes are on different lines.  Consider a 3 line printf
statement, where the starting quote is on the first line, and the
ending quote is on the third, and the "problem" string is on the
second.

This is quite valid code, but it requires that one parse the statement
character by character.

Life is never easy.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 20 Nov 2002 03:00:00 GMT  
 Rookie AWK Question......

Quote:



<snip>

>>The way I've solved this type of problem in the past is to copy $0 and
then
>>delete quoted strings from the copy using a suitable regex and gsub(),
then
>>you can identify whether the // is inside quotes or not.

>While that will work for many cases, it does not work when the starting
>and ending quotes are on different lines.  Consider a 3 line printf
>statement, where the starting quote is on the first line, and the
>ending quote is on the third, and the "problem" string is on the
>second.

>This is quite valid code, but it requires that one parse the statement
>character by character.

Agreed, which is one reason why I prefer not to split printf strings over
more than one line in my own code (whether it's awk or c).  If by some
mischance I _do_ have to deal with this then I resort to sed to accommodate
the multi-line strings and pipe the output to awk for further processing.

Quote:
>Life is never easy.  :-)

<snip>

You wouldn't have it any other way now would you?  Easy can be too boring!

Incidentally a _lot_ of REXX interpreters get the "start of comment" in a
string problem wrong, so that the code isn't parsed correctly.

Something like this goes wrong:

...
/* a normal comment */
say "a string including /* - not a comment"
...

giving error message from the interpreter about missing closing quotes or
possibly unterminated comment (depending on subsequent code and the
interpreter in use!  Heigh Ho!

Pity, because it's a pretty useful language in many ways - I use it all the
time at work on IBM mainframes and it runs on all the boxes that I've used.

Peter
-- --


Opinions expressed are my own and not necessarily those
of my employer



Thu, 21 Nov 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Rookie question

2. Rookie question about Literals

3. Rookie Dictionary Question

4. A very, very rookie question...

5. Rookie question about LIM module

6. Assembly rookie needs help--2 easy questions

7. Rookie VHDL FSM Question

8. rookie question

9. rookie question: the do-loop in F77

10. Rookie question: "Replace"-command?

11. Rookie question on FORTRAN 90 Pointers

12. Rookie question on FORTRAN 90 Pointers

 

 
Powered by phpBB® Forum Software