in reply to Re: Printing largest number in array using loops and if statement
in thread Printing largest number in array using loops and if statement

Hmm. I must admit i do not fully understand that line of code, perhaps if written in more lines it is better to understand:

$max = $_ if ! defined $max || $_ > $max;
Could one say if $max is not defined (set to a value maybe this could occure on first iteration?). or $_ is greater than $_max?. Then $max is set to $_?

  • Comment on Re^2: Printing largest number in array using loops and if statement
  • Download Code

Replies are listed 'Best First'.
Re^3: Printing largest number in array using loops and if statement
by dsheroh (Monsignor) on Jan 17, 2010 at 11:53 UTC
    Yes, that is what it means. Any time you have stmt if cond, it can be rewritten as
    if (cond) { stmt; }
    So, in this case, that line is equivalent to
    if (! defined $max || $_ > $max) { $max = $_; }
    Personally, I prefer the method of initializing $max to the first (defined) item in the list so as to avoid testing !defined $max on every pass. The performance benefit of avoiding that test is negligible, of course, but it reads more cleanly to me when you're able to just say "remember this as the highest value if it's greater than the previous highest value" without the extra "and a highest value has been previously set" clause.