Richard Monson-Haefel wrote:
Hi,

Hello,

I'm writing a paper comparing the use of Perl, Python and Java. My Perl
is pretty bad (as is my Python), so I thought I would check to see if
there is a more efficient way of doing it.
Here is the Perl program


http://www.rafb.net/paste/results/4px8PI74.html

1) strict will help you avoid mistakes so I added it (perldoc strict)

2) I got rid of all the foreach's and extra variable declarations and some unnecessary brackets and what not:

#!/usr/bin/perl -w

use strict;

print "@ARGV\n";
my @r;
for(@ARGV){
   push(@r, $_) if length($_) <= 3;
}
print "@r\n";
print "$_\n" for @r

To even optimise more you could remove use strict and -w after you're sure it strict and warnings safe. (which it is)

Or if you don't need to save the original @ARGV:

#!/usr/bin/perl -w
use strict;
print "@ARGV\n";
@ARGV = grep { length($_) <= 3 } @ARGV;
print "$_\n" for @ARGV;

Also strict and warning safe :)

if you change the first print to
print "@ARGV\n" unless [EMAIL PROTECTED];
you'll avoid a blank line as output if no argumnets are given..

See? Perl is way sexxier that the other guys ;p

HTH :)

Lee.M - JupiterHost.Net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to