perl

How can I find the largest number from a list where the first and last digits are always treated as 0?


Sample data is listed below.

                                      '3.268160E-02',

                                           '2.433833E-02',

                                           '2.473959E-01',

                                           '4.960246E-06',

                                           '1.470189E-05',

                                           '1.527312E-05',

                                           '8.997553E+01',

                                           '8.981999E+01',

                                           '2.700648E+02',

                                           '2.656095E+02',

                                           '2.719822E+02',

                                           '2.695895E+02'

For example, if I have a list [2, 4, 3], I want to check the following conditions:

Check if 2 is greater than 0 (the first number in the list) or if 2 is greater than 4 (the next number in the list). Check if 4 is greater than 2 (the previous number) or if 4 is greater than 3 (the next number). Check if 3 is greater than 4 (the previous number) or if 3 is greater than 0 (the last number in the list).

How can I implement this?


Solution

  • If you want to treat those as numbers, just do. Perl will do it automatically. You can either simple sort them:

    my @sorted_list = sort { $a <=> $b } @numbers;
    

    But keep in mind that sorting is expensive and costs way too much if you just want the max.

    Or you can use List::Util to find the greatest or smallest value.

    use List::Util qw(max min);
    say max (@numbers);
    say min (@numbers);