By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

Read a line by characters

P: 50
I'm reading line by line and making comparisons. I have been using the index function but I knew what my substring was. Now I don't know what my substring is. I have an index function searching for ' /* ' and as soon as I see this comment, I want to know if there is anything before it on the line.

For example:

Expand|Select|Wrap|Line Numbers
  1. print "hello"; /* prints hello */
  2.  
I'm trying to determine if there is a line of code before the comment starts. Now I have this approach done by searching for a semicolon. Then comparing the index of the semicolon to the index of /* and checking if the index of the semicolon is lower than the the index of the /* which means that there is a line of code. Now I have this problem what if there is no semicolon there such as

print "hello" /* prints hello */
;

This line of code will still execute but I will not know if there is something before the /*. My question is, is there someway of reading the first character in the line and comparing that to / to determine if there is something before the /*

Psuedocode:

Expand|Select|Wrap|Line Numbers
  1. if (index($_, "/*"))
  2. {
  3. #now check to see if there is something before it
  4.  
  5. if(first character of line == "/")
  6. {
  7.     print "yes there is something before /* ";
  8. }
  9.  
  10. }
  11.  
Any help would be greatly appreciated. Thanks in advance. In the meantime I will continue to look for a solution. If I find one, I will post the answer.
Apr 10 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
Since you are really searching for a pattern a regexp is appropriate:

Expand|Select|Wrap|Line Numbers
  1. @lines = ('print "hello"; /* prints hello */', 'print "hello";');
  2. foreach my $line (@lines) {
  3.    if ($line =~ m#^.+?/\*.*?\*/#){
  4.        print "There is a comment: $line\n";
  5.    }
  6.    else {
  7.        print "There is no comment: $line\n";
  8.    }
  9.  
I didn't put too much thought into the regexp so it could maybe be improved upon.
Apr 10 '08 #2

P: 50
Thanks a lot for the solution, I looked into it when you gave me the recommendation for the regex. This is the solution I came up with. I strip all the leading whitespaces and check if it's a comment or not. That's how I know it's a line of code. There is one flaw if the comment is before the line of code (/* */ code;) But that's a case that I'm not looking for.

Expand|Select|Wrap|Line Numbers
  1.  
  2. s/^\s+//; #strip leading whitespace
  3.  
  4. if(/^[^\/*]/)
  5. {
  6.     $line_count++;
  7. }
  8.  
  9.  
Apr 16 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.