Please support our Perl advertiser: Programming Forums
Views: 2470 | Replies: 8
![]() |
•
•
Join Date: Jun 2005
Posts: 17
Reputation:
Rep Power: 4
Solved Threads: 0
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
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
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:
Now, the array numarray will contain those numbers.... and you can do a:
Or Reference The Array in Scalar context, such as $numarray[0]. Let me know how that works for you.
($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.
•
•
Join Date: Jun 2005
Posts: 17
Reputation:
Rep Power: 4
Solved Threads: 0
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+))/){•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
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.
•
•
Join Date: Jun 2005
Posts: 17
Reputation:
Rep Power: 4
Solved Threads: 0
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






leaning toothpick syndrome.... gotta love it
Linear Mode