| | |
Finding a number in a line
![]() |
•
•
Join Date: Jun 2005
Posts: 17
Reputation:
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?
Perl Syntax (Toggle Plain Text)
-- | |- state (1)^M
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.
Perl Syntax (Toggle Plain Text)
($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:
Perl Syntax (Toggle Plain Text)
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:
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.
Perl Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jun 2005
Posts: 17
Reputation:
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
![]() |
Similar Threads
Other Threads in the Perl Forum
- Previous Thread: Log file rotation script
- Next Thread: noobie asking for assistance with file parsing...
| Thread Tools | Search this Thread |







leaning toothpick syndrome.... gotta love it