The use of 'sort' to find a max is always overkill, but can be convenient if efficiency is not an issue. In your case, you probably want 'nsort_by' from the module List::SomeUtils instead of the native 'sort'.

You should be more explicit in describing what you mean by 'number'. Your code finds only continuous strings of decimal digits. (Note: This ignores negative signs. e.g. -124 > 123)

An easy way to find the maximum of the numbers on one line is to use the function 'max' from List::Util

>type SSSufe.pl use strict; use warnings; use List::Util qw(max); use List::SomeUtils qw(nsort_by); my @lines = <DATA>; print ((nsort_by { biggest_on_line($_) } @lines)[-1]); sub biggest_on_line { my $line = shift; my @nums = $line =~ /(\d+)/g; return max @nums; } __DATA__ Hello, i'm 18 1 this year is 2019 1 1 2 3 - 4 >perl SSSufe.pl 1 this year is 2019 1
Bill

In reply to Re: Print the line with the largest number from standard input by BillKSmith
in thread Print the line with the largest number from standard input by SSSufe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.