Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: split string using optimized code or perl one liner

by davido (Cardinal)
on Feb 22, 2017 at 16:05 UTC ( [id://1182538] : note . print w/replies, xml ) Need Help??


in reply to split string using optimized code or perl one liner

If efficiency is defined by keystrokes, you may save a few by using capturing parens in your split. You may also rely on the fact that the -n flag loads one line at a time into $_, and that split looks at $_ if no other string param is provided:

echo www.perlmonks.org.split.reduce.code.check |perl -lnE 'say reverse + split /(\.)/'

So this is 36 keystrokes if you start counting after the pipe. Someone will probably beat it though. One way to beat it will be eliminating two unneeded whitespace characters, so this gets it down to 34:

echo www.perlmonks.org.split.reduce.code.check |perl -lnE'say reverse +split/(\.)/'

If efficiency is defined by some other metric, you would probably need to benchmark a few alternatives according to that metric to determine which one wins.


Dave