in reply to extract numbers from unformatted text strings

You can't count on knowing what the separator is going to be. It could be a string containing pretty much anything other than digits. Therefore:
use strict; use warnings; my $min = 1000000; my $max = 0; while (<DATA>) { m/(\d+)\D+(\d+)/; $min = $1 if $1 < $min; $max = $2 if $2 > $max; } print "$min-$max"; __DATA__ 6-7 1 - 5 3 to 24 6 thru 9 2 through 11

Replies are listed 'Best First'.
Re^2: extract numbers from unformatted text strings
by fishbot_v2 (Chaplain) on May 23, 2005 at 17:54 UTC

    Nice, but don't forget the first rule of a capturing m// - test for success before using $1...

    while (<DATA>) { next unless m/(\d+)\D+(\d+)/; # or warn && next, or die $min = $1 if $1 < $min; $max = $2 if $2 > $max; }

    Also, rather than imposing an artificial limit on min (and max), what about addind a definedness check?

    $min = $1 if ( ! defined( $min ) || ( $1 < $min ) ); $max = $2 if ( ! defined( $max ) || ( $1 > $max ) );

    I'll grant that the since this doesn't handle negative numbers (yet) anyway, max is likely fine the way it is.