in reply to Simple Perl, to get me started.

One way is to use sort and length:
use strict; use warnings; my @args = sort { length($b) <=> length($a) } @ARGV; print "longest is $args[0], length = ", length($args[0]), "\n"; __END__ longest is ert, length = 3

See also: How do I compose an effective node title?

Replies are listed 'Best First'.
Re^2: Simple Perl, to get me started.
by Lust69 (Initiate) on Oct 27, 2010 at 15:54 UTC
    Thanks for the working code, is there a way to do the same job without these new things, like use strict and use warnings and sort? The code before has no mension of these, so im thinking how out of thin air was I meant to have known these from the past codes.

        For an analogy:
        You got this one recipe that needs adjusting from an eccentric guy with only a poofy hat and a spatula...

        use strict; # Like wearing pants in the kitchen use warnings; # Shirts are good when the boiling grease is flying too

      The problem can't be solved with only the functions and operators in the code you were asked to review - you must use something else. I can't tell you what the author of the lab your are studying intended, but learning to think and research independently will be more valuable in the long term than the Perl you will learn.

      Sometimes it is helpful to try to solve a problem with a limited set of tools and sometimes this is required in an academic assignment. If it is, it should be obvious and you should try to solve the problem with the restrictions. Even outside the academic environment you will find yourself limited by seemingly arbitrary and poorly informed decisions of others. Otherwise, there is rarely a restriction on the research and learning you can do, other than the number of hours in the day. Others have pointed out resources available here and elsewhere on the Internet. There are excellent books available. The initiative to find them and learn is yours.

      is there a way to do the same job without these new things, like use strict and use warnings
      Yes, but you will regret it... The strictures, according to Seuss
      and sort?
      You don't have to use the Perl built-in sort function. You are at liberty to determine the maximum length with your own code. I just showed you one common way to do it.