Here is a fairly simple solution that nobody seems to have suggested. It processes the data line by line, pulling all the integers from each line into an array using a simple regex with the g quantifier, then reverse sorts the array, and finally prints the first element. The code is probably a bit more verbose than it needs to be, but that is deliberate as you mention that you are new to
PERL Perl.
#!/usr/bin/perl -w
use strict;
# Read each line one by one
while (my $line = <DATA>) {
chomp($line); # Dispense with trailing newline
# Extract everything that looks like an integer into an array
my @ints = $line =~ m/(\d+)/g;
# Sort the array, highest to lowest
my @sorted_ints = reverse sort { $a <=> $b } @ints;
# Ouput the line, and the highest integer (1st element of the sort
+ed array)
print "DATA:$line\nHIGHEST INTEGER:$sorted_ints[0]\n";
}
__DATA__
ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 DAFFEE 376
ASB C 134 PPKOREAK EFEAF 290
BLAH 99 BLAH 123 FRED 27 BARNEY 427
Output:
DATA:ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 DAFFEE 376
HIGHEST INTEGER:1090
DATA:ASB C 134 PPKOREAK EFEAF 290
HIGHEST INTEGER:290
DATA:BLAH 99 BLAH 123 FRED 27 BARNEY 427
HIGHEST INTEGER:427
Hope this helps,
Darren :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.