Finding a number in a line

Reply

Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Finding a number in a line

 
0
  #1
Jun 9th, 2005
If I have a line like this, how do I exclude all other lines that don't contain a number in this format? and how do I input this number into an array?


  1. -- | |- state (1)^M
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: Finding a number in a line

 
0
  #2
Jun 9th, 2005
The Number in the ()? In This Case 1?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

 
0
  #3
Jun 9th, 2005
yes, that is the number
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: Finding a number in a line

 
0
  #4
Jun 9th, 2005
I would personally use split..... if you know that the line will look like that most of the time, it's easy enough to split it like so:
  1. ($junk, $rest) = split(/\(/, $_); # $_, or whatever variable contains the string
  2. push @numarray, substr($rest, 0, 1);

Now, the array numarray will contain those numbers.... and you can do a:
  1. foreach $num (@numarray) {
  2. print "$num\n";
  3. }

Or Reference The Array in Scalar context, such as $numarray[0]. Let me know how that works for you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

 
0
  #5
Jun 9th, 2005
ok, that helps for making the array. Is there some way to use an if statement to only get the lines that have a "(number)" in them in that form? Something like that where d+ is the number I am looking for.

  1. if($line =~ /((d+))/){
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: Finding a number in a line

 
0
  #6
Jun 9th, 2005
Sure, but you should only need 1 set of parenthesis, and you might have to escape them, I'm not sure... (by escaping, I mean making it a literal character using \, such as \(, but I'm not positive you have to). But you have the idea straight up.... just search the string for what you are looking for, and then run the split if it's true. You could also look at using your own custom function, but I think you hit the nail on the head with the pattern matching regex above.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

 
0
  #7
Jun 10th, 2005
If I put the backslash before the parentheses, it gives me an error saying there is an unmatched paren. If I leave the backslash out, it runs, but it doesn't give me all the data it should. It is cutting out some of the lines that contain that character. I can e-mail you a copy of the output file and the original file if you like, but it would be too long to put on here
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

 
0
  #8
Jun 10th, 2005
IT WORKS!!!!! I was missing a few backslashes. Thanks for your help!
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: Finding a number in a line

 
0
  #9
Jun 10th, 2005
leaning toothpick syndrome.... gotta love it
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC