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

Recommended Answers

All 8 Replies

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

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.

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+))/){

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.

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.