Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

making a shorter code!

by husoft (Monk)
on Sep 14, 2002 at 04:45 UTC ( #197789=perlquestion: print w/replies, xml ) Need Help??

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

I saw an exercise in the book 'Learn Perl in 21 days':
-------------------------------------------------------- #!/usr/bin/perl print ("Enter the distance to be converted:\n"); $originaldist = <STDIN>; chop ($originaldist); $miles = $originaldist * 0.6214; $kilometers = $originaldist * 1.609; print ($originaldist, " kilometers = ", $miles, " miles\n"); print ($originaldist, " miles = ", $kilometers, " kilometers\n"); --------------------------------------------------------
And wanted to make it shorter...
-------------------------------------------------------- #!/usr/bin/perl print "Enter the distance to be converted:\n"; chomp($_=<>);print"$_ kilometers= ".$_*0.6214." miles\n$_ miles= ".$_*1.609." kilometers\n"; --------------------------------------------------------
Is there a posible way to make it even SHORTER?!

Replies are listed 'Best First'.
Re: making a shorter code!
by Aristotle (Chancellor) on Sep 14, 2002 at 04:56 UTC
    I don't see any. I don't see any reason for compressing it like that though - life is not a JAPH contest. Getting rid of temporary variables where sensible is good - getting rid of legibility is not. And while it may just seem like nitpickery here, make sure to get in the habit of strict and warnings. I'd write this as follows:
    #!/usr/bin/perl -w use strict; print "Enter the distance to be converted:\n"; chomp($_=<>); print "$_ kilometers= ", $_*0.6214, "miles\n", "$_ miles= ", $_*1.609, " kilometers\n";
    For this example you could take advantage of the -l switch to Perl (see perldoc perlrun) to get rid of some line noise (pay attention to the shebang line):
    #!/usr/bin/perl -wl use strict; print "Enter the distance to be converted:"; $_=<>; print "$_ kilometers= ", $_*0.6214, "miles"; print "$_ miles= ", $_*1.609, " kilometers";

    Makeshifts last the longest.

      And while it may just seem like nitpickery here, make sure to get in the habit of strict and warnings. I'd write this as follows
      At the risk of being nit-picky as well, I think it should be emphasized that things like using strict and enabling warnings etc., while generally helpful, are not forced upon the user in perl for a reason - so why should we try to enforce them on a cultural level when we don't do it on the technical one? It's all about choice.

        Well, I don't use strict for my oneliners. :-) As with many other things, you should stick to accepted good practice unless you understand why it is accepted good practice. (And even then you'll find yourself sticking to it most of the time.)

        Perl6 will be stricter than Perl5 by default for sensible cases, which I'm looking forward to.

        Makeshifts last the longest.

Re: making a shorter code!
by Zaxo (Archbishop) on Sep 14, 2002 at 05:42 UTC

    No. You improved it with chomp. <> for <STDIN> is worse since it lets in data from files named on the command line. Most of the verbiage is from words you want to print.

    After Compline,
    Zaxo

      <> for <STDIN> is worse since it lets in data from files named on the command line.

      You can not protect <STDIN> from redirection on the command line either.

Re: making a shorter code!
by rir (Vicar) on Sep 14, 2002 at 14:15 UTC
    Just eliminate some write space. Use some variables. Use the -l flag.
    You could use a variable and / and * but
    rounding differences will creep in.
    Five newlines may be removed from the following.

    I don't do this kind of thing much and tend
    not to recommend it to beginners. That is my bias.

    I think there are two paths to Perlish wisdom ,
    on one you will, if necessary, flip hamburgers for minimum
    wage to buy disk space and random access memory
    to avoid having to scrunch beauteous code into small
    spaces.
    The short path is to sit in front of the keyboard
    and learn all the weirdities of perl.
    The short path is only shorter in that you sit in
    one place a lot.

    Perhaps if you can come and go at the same time. And
    hear the difference between a symbol table alias
    and hard ref. Then you should take both paths.

    The nature of the enlightenment achieved varies depending
    upon which path you take.

    #!/usr/bin/perl -l print"Enter the distance to be converted:"; $_=<>; chomp; $k=" kilometers"; $m=" miles"; print"$_$k= ".$_*0.6214."$m\n$_$m= ".$_*1.609."$k";
Re: making a shorter code!
by tlhf (Scribe) on Sep 15, 2002 at 00:51 UTC
    #!/usr/bin/perl -l print"Enter the distance to be converted:"; printf"$_ kilometers= %f miles\n$_ miles= %f kilometers",($_=<>)*0.621 +4,$_*1.609;

    Is the shortest I could think of in a short period of time. At 144 bytes. The chomp seemed unecessary (Perl 5.6.1/Win32), and -l allowed me to skip \ns, and using printf allowed the three chars of %f and a commer, rather than "..". I didn't seem to get much benefit by using $m=miles or $m=' miles' tho.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://197789]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2021-10-20 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (81 votes). Check out past polls.

    Notices?