954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding a number in a line

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
bigfoot_80906
Newbie Poster
17 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

The Number in the ()? In This Case 1?

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

yes, that is the number

bigfoot_80906
Newbie Poster
17 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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+))/){
bigfoot_80906
Newbie Poster
17 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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

bigfoot_80906
Newbie Poster
17 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

IT WORKS!!!!! I was missing a few backslashes. Thanks for your help!

bigfoot_80906
Newbie Poster
17 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

;) leaning toothpick syndrome.... gotta love it

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You