Ignoring the rest of a line

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Ignoring the rest of a line

 
0
  #1
Apr 28th, 2005
Does anyone know how to ignore the rest of a line in Perl? I am reading a file and printing out tokens to an output file. I want to ignore the rest of a line after a colon is found, which indicates a comment. That way it will not print to the output file. I tried a range from (; - \n) but it does not work. Thanks for any input.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Ignoring the rest of a line

 
0
  #2
Apr 28th, 2005
My personal suggestion, would be to do a split or regex on it. I would do a split, but I'm a bit silly. The thing is, if a colon starts a comment, then there can be no colon's within the "good string". what I mean is, if the line has:
  1. she said: how are ya :this is the comment

then "she said" would be the only thing that would not be a comment. But, This is how I would do it:

  1. ($goodinfo, $comment) = split(/:/, $_) # you can replace $_ with the variable you use
  2. print FH "$goodinfo\n";

obviously, the above line will need modifications in order to suite what it is you are doing, but split line works by searching for a colon, if it finds one, it sticks the stuff on the left of the colon, into the first variable, and the stuff on the right in the second variable (those that are in parenthesis). You could have it return an array, also, and just do a pop on the array, but in that way, only the data after the very last colon in the string would be ignored. Let me know what you come up with.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Ignoring the rest of a line

 
0
  #3
Apr 28th, 2005
I wouldn't normally double post, but I found the best way that I would do it, and converted it into a sub that you can use:

  1. sub strip_comment
  2. {
  3. # /* Get The Parameter Passed To The Function */
  4. $theline = shift;
  5.  
  6. # /* Search For The First Occurance Of : */
  7. $where = index($theline, ":");
  8.  
  9. # /* If We Find No Comment, Return The Entire Line Passed To us */
  10. if ($where eq -1) {
  11. return $theline;
  12. }
  13.  
  14. # /* Get The Part Of The String Up To, And Including The Colon */
  15. $goodpart = substr($theline, 0, $where);
  16.  
  17. # /* Remove The Colon */
  18. chop($goodpart);
  19.  
  20. # /* Return Only The Portion Of The String Before The Colon */
  21. return $goodpart;
  22. }

just stick that at the bottom of your perl file, and then call it like this:

  1. $retval = &strip_comment($theline);

Assuming $theline was the entire line, comments and all, then $retval would contain only the non-comment part of the line. Let me know how it works for you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3318 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC