in reply to Help with regex, how to get the largest integer in a string?

Hi coltman,

You can iterate over a given string using the /g (global) quantifier, as well as regex captures:

use strict; use warnings; my $string = "ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 D +AFFEE 376"; my $max; # This is undefined until set while ($string =~ /(\d+)/g) { if (!defined($max) or $max < $1) { $max = $1; } } print "Max value is $max\n";

Or you could make it into a subroutine (eg. max_val) like so:

use strict; use warnings; my $string = "ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 D +AFFEE 376"; my $max = max_val($string); print "Max value is $max\n"; sub max_val { my $str = shift; my $max; while ($string =~ /(\d+)/g) { if (!defined($max) or $max < $1) { $max = $1; } } return $max; # This will be undefined if no integer found }

And, of course, you can read more about regular expressions with perlretut and perlre.

Update:  Oh, and perlrequick.  I always forget that one.

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/