944,018 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 3704
  • Perl RSS
Jun 9th, 2005
0

Finding a number in a line

Expand Post »
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)
  1. -- | |- state (1)^M
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigfoot_80906 is offline Offline
17 posts
since Jun 2005
Jun 9th, 2005
0

Re: Finding a number in a line

The Number in the ()? In This Case 1?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 9th, 2005
0

Re: Finding a number in a line

yes, that is the number
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigfoot_80906 is offline Offline
17 posts
since Jun 2005
Jun 9th, 2005
0

Re: Finding a number in a line

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

Now, the array numarray will contain those numbers.... and you can do a:
Perl Syntax (Toggle Plain Text)
  1. foreach $num (@numarray) {
  2. print "$num\n";
  3. }

Or Reference The Array in Scalar context, such as $numarray[0]. Let me know how that works for you.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 9th, 2005
0

Re: Finding a number in a line

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)
  1. if($line =~ /((d+))/){
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigfoot_80906 is offline Offline
17 posts
since Jun 2005
Jun 9th, 2005
0

Re: Finding a number in a line

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 10th, 2005
0

Re: Finding a number in a line

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

Re: Finding a number in a line

IT WORKS!!!!! I was missing a few backslashes. Thanks for your help!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigfoot_80906 is offline Offline
17 posts
since Jun 2005
Jun 10th, 2005
0

Re: Finding a number in a line

leaning toothpick syndrome.... gotta love it
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Log file rotation script
Next Thread in Perl Forum Timeline: noobie asking for assistance with file parsing...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC