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

I have been told to review these peices of code:
#commandline.pl #\!C:\strawberry\perl\bin\perl.exe $y=0; for $x (@ARGV) {print "argument $y is $ARGV[$y] \n";$y++;}
#split.pl #\!C:\strawberry\perl\bin\perl.exe while($input=<STDIN>) { @line=split(" " ,$input); $i=0; for $word (@line) { $len=length($word); print " word $i = '$word' has a length of $len \n"; $i++; } }
Now I must modify the code above and the question is... Write a perl script which prints out the value and length of its longest command line argument. For example, running the script with argument a b df ert and y produces,

>perl longest.pl a b df ert y

>perl longest argument is "ert" - length = 3

Can someone modify the code above to do this for me (I have tried), I am learning alone this is not homework but in lab questions that do not have to be completed. I obviously want to learn.

Replies are listed 'Best First'.
Re: Simple Perl, to get me started.
by toolic (Bishop) on Oct 27, 2010 at 15:48 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.

        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.
Re: Simple Perl, to get me started.
by jethro (Monsignor) on Oct 27, 2010 at 16:46 UTC

    The best way to learn is to do. If we just give you the answer you haven't learned anything.

    You know you have to combine these two scripts, because the first uses command line arguments to do something else and the second has a nice loop you could use.

    So just do it. Try to take from both scripts the things you need and then add inside the loop some way to remember the longest string that you see. If it doesn't work, show us what you got and we will help you from there.

      jethro's point is well taken... and to take it one (possibly irrelevant) step further (because these snippets to review smell a lot like homework; a take-home quiz; or job interview testing):

      If you get the job because we gave you the answers, you won't keep it for long... deservedly!

      And re your hesitance at introducing warnings and strict, were I still interviewing and handing out code like this for "review," applicants who failed to add strict and warnings would fall far down the preference list.

      Oh, yes; my candidates got extra points off for inability to spell things like "pieces."

Re: Simple Perl, to get me started.
by MidLifeXis (Monsignor) on Oct 27, 2010 at 16:03 UTC

    It looks like you have all of the pieces necessary to do your task except for if. See length, print, split, etc for details on the commands being used.

    For the part you have not been shown: if (BOOL) {...} elsif (BOOL) {...} else {...}

    --MidLifeXis

Re: Simple Perl, to get me started.
by ig (Vicar) on Oct 27, 2010 at 17:49 UTC

    Here is another way to solve the problem, using only basic functions and operators - very little other than what is in your original programs.

    use strict; use warnings; my $longest = ''; for my $x (@ARGV) { if(length($x) > length($longest)) { $longest = $x; } } my $length = length($longest); print "longest argument is '$longest' - length = $length\n";

    Here are a couple more issues for you to think about:

    What should your program do if there are more than one arguments of the same, longest length. For example, what if the arguments were: abc def ghi?

    What should your program do if there are no arguments?