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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |