RSS Forums RSS
Please support our Perl advertiser: Programming Forums
Views: 2470 | Replies: 8
Reply
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Finding a number in a line

  #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?


--                             |       |- state (1)^M
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Finding a number in a line

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

Re: Finding a number in a line

  #3  
Jun 9th, 2005
yes, that is the number
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Finding a number in a line

  #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:
($junk, $rest) = split(/\(/, $_); # $_, or whatever variable contains the string
push @numarray, substr($rest, 0, 1);

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

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

Re: Finding a number in a line

  #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.

 if($line =~ /((d+))/){
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Finding a number in a line

  #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  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

  #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  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Finding a number in a line

  #8  
Jun 10th, 2005
IT WORKS!!!!! I was missing a few backslashes. Thanks for your help!
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Finding a number in a line

  #9  
Jun 10th, 2005
leaning toothpick syndrome.... gotta love it
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:57 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC