CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Matching second value in a line


New User

Feb 7, 2013, 8:03 AM

Post #1 of 4 (1470 views)
Matching second value in a line Can't Post

I am editing a program to scrape data from a given website. Currently, it searches for a given phrase and then grabs the next number it sees. For example, if that website has:

Temperature 28 30

the program grabs '28' with the following:

($obd =~ m/Temperature.*?>\s*([0-9\.]+)\s*/is)[0],

I need the program to grab the second number. How do I do this given that the first number will not be constant?


Feb 7, 2013, 9:45 AM

Post #2 of 4 (1463 views)
Re: [agdodson1] Matching second value in a line [In reply to] Can't Post

use strict; 
use warnings;
use 5.14.0;
my $odb = 'Temperature 28 30';
my $temperature2 = $odb =~ s/Temperature \D+ \d+ \D+ (\d+) /$1/rx;
say $odb;
say $temperature2;

Good Luck,

Veteran / Moderator

Feb 8, 2013, 7:56 AM

Post #3 of 4 (1448 views)
Re: [BillKSmith] Matching second value in a line [In reply to] Can't Post

I know that the /x modifier is part of the recommended best practices, but I nonetheless think that adding spaces within a regular expression does not help readability when spaces are part of what you want to match in the string.

I would write the relevant line as follows:

my $temperature2 = $1 if $odb =~ /Temperature\s+\d+\s+(\d+)/;

Or even:

my $temperature2 = $1 if $odb =~ /Temperature +\d+ +(\d+)/;

BTW, all these regexes will fail if the temperature values may contain a decimal point. If such is the case, change "\d" to a character class such as [\d.], as in the following example:

$t = "temp 43.5 37.2"; 
print "$1 $2\n" if $t =~ /temp ([\d.]+) ([\d.]+)/; # prints "43.5 37.2"


Feb 8, 2013, 1:04 PM

Post #4 of 4 (1442 views)
Re: [Laurent_R] Matching second value in a line [In reply to] Can't Post

My regular expression was not intended to be a great solution. It was intended to emphasize the concept of match every field and return the one of interest. I felt that the unnecessary whitespace helped make that point. "Best Practice" requires matching whitespace explicitly. From that viewpoint, It would have been better had I used \s+ rather than \D+.

Many of our readers would find a true "Best Practice" solution to be obscure. You seem to have a good compromise.
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives