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

Simple is usually fastest in perl.

#! perl -slw use strict; use List::Util qw[ max ]; my %maxs; $maxs{ $. } = max m[(\d+)]g while <DATA>; print "line: $_ max: $maxs{ $_ }" for sort keys %maxs; __DATA__ ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 DAFFEE 376 ASB C 134 PPKOREAK EFEAF 290 A 100 B 1000 C 2000 D 3000 E 4000 F 5000 G 6000 H 7000 I 8000 J 9000 +K 10000 L 100000 M 200000 N 2

Output:

c:\test>junk4 line: 1 max: 1090 line: 2 max: 290 line: 3 max: 200000

Update: Using an array for output presentation, instead of a hash (as implied by ysth++ below):

#! perl -slw use strict; use List::Util qw[ max ]; my @maxs; $maxs[ $. ] = max m[(\d+)]g while <DATA>; print "line: $_ max: $maxs[ $_ ]" for 1 .. $#maxs; __DATA__ ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 DAFFEE 376 ASB C 134 PPKOREAK EFEAF 290 A 100 B 1000 C 2000 D 3000 E 4000 F 5000 G 6000 H 7000 I 8000 J 9000 +K 10000 L 100000 M 200000 N 2

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Help with regex, how to get the largest integer in a string?
by ysth (Canon) on Apr 19, 2007 at 07:55 UTC
    A hash?!