Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please help me understand what I need to change in the beggining:
#!/usr/bin/perl -w my $max_number=''; #initialize a variable that keeps the maximu +m number my $current_number=''; #variable that will keep the number of eac +h line for comparison while (defined ($current_number=<STDIN>)) { chomp ($current_number); if($current_number > $max_number) { $max_number = $current_number; #compare each time t +he number we have in the line with the max number } } print "The maximum number of all in your file is: ".$max_number."\n";

The error I get is:
Argument "" isn't numeric in numeric gt (>) at myscript.pl line 9, <ST +DIN> line 1.

The program works, but I think I did not set my $max_number=''; correctly... What should I put there?

Replies are listed 'Best First'.
Re: What is wrong in this code?
by Athanasius (Archbishop) on Feb 13, 2014 at 15:33 UTC

    Some additional observations:

    1. No need to declare $current_number before the loop. It’s a lexical variable which is not used outside of the loop, so declare it in the while condition:

      while (my $current_number = <STDIN>)
    2. No point in testing whether $current_number is defined: anything returned by <STDIN> will contain a newline (that’s why you chomp it!), so must be defined.

    3. You say the script works, but there is no elegant way for the user to break out of the loop. Give the user an option, for example “Q” for quit, and test for it after the chomp:

      last if $current_number eq 'Q';
    4. You should add print statements to prompt the user for input, otherwise how are they supposed to know what to do?

      print "Enter a number ('Q' to quit):\n";

      You will need to add this print statement twice, once immediately before the loop and once at the end.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: What is wrong in this code?
by hdb (Monsignor) on Feb 13, 2014 at 15:07 UTC

    Do not initialize your $max_number:

    my $max_number; #initialize a variable that keeps the maximum n +umber

    so you can check for undef in your loop:

    if(!defined($max_number) or $current_number > $max_number) + { $max_number = $current_number; #compare each time t +he number we have in the line with the max number }
      syntax error at myscript.pl line 3, near "=;"
      It's the line with my $max_number;

        Just remove the = sign.

Re: What is wrong in this code?
by toolic (Bishop) on Feb 13, 2014 at 15:05 UTC
    Initialize your $max_number to the minimum value you expect. For example, if you only expect positive numbers, set it to 0:
    my $max_number=0; #initialize a variable that keeps the maximum + number

    Another approach is to store all values into an array, then use List::Util max . This assumes memory is not an issue.

      Yeah, thing is that the file can contain both, so what can I do in this case?
      (but not use Perl modules which is the obvious solution)
        but not use Perl modules
        List::Util is a Core module, so there is no need to install it.
Re: What is wrong in this code?
by pajout (Curate) on Feb 13, 2014 at 15:12 UTC
    Or just change your line 9 to
    if($max_number eq '' or $current_number > $max_number)
      Ah, it works in that way, thanks !