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

Re^3: Perl Golf idea

by JavaFan (Canon)
on May 01, 2012 at 14:57 UTC ( #968277=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Perl Golf idea
in thread Perl Golf idea

One character less:

perl -nlE'sub w{[sort/./g]}INIT{$w=w$_=pop}$w~~w&&say' ./dict.txt acer +t
And if you don't mind the (second) argument getting printed, another 3 chars less:
perl -nlE'sub w{[sort/./g]}$w||=w$_=pop;$w~~w&&say' ./dict.txt acert


Comment on Re^3: Perl Golf idea
Select or Download Code
Replies are listed 'Best First'.
Re^4: Perl Golf idea
by brx (Pilgrim) on May 01, 2012 at 20:58 UTC
    And if you don't mind the (second) argument getting printed, another 3 chars less:
    perl -nlE'sub w{[sort/./g]}$w||=w$_=pop;$w~~w&&say' ./dict.txt acert

    This one is wrong (only) if the first line of 'dict.txt' contains a valid word (different from arg2).

    example:

    $ cat dict.txt one neo noe bad $ perl -nlE'sub w{[sort/./g]}$w||=w$_=pop;$w~~w&&say' ./dict.txt oen oen neo noe

    First word is missing. Anyway, this is a very beautiful version: ++

    UPDATE: with an extra newline

    perl -E'@a=sort pop=~/./g;say grep@a~~[sort/./g],<>' ./dict.txt acert

    UPDATE2: one char useless

    perl -E'@a=sort pop=~/./g;say grep@a~~[sort//g],<>' ./dict.txt acert

      Nice. Here's one that returns the correct output using a few characters less:

      perl -nE'$w//=pop;[sort$w=~/./g]~~[sort//g]&&print' ./dict.txt acert

      or, two characters less than that, but with an extra newline between each word:

      perl -nE'$w//=pop;[sort$w=~/./g]~~[sort//g]&&say' ./dict.txt acert

      UPDATE: shorter version with correct output:

      perl -nlE'$w//=pop;[sort$w=~/./g]~~[sort//g]&&say' ./dict.txt acert

      or, same length, more efficient:

      perl -nlE'($w//=[sort pop=~/./g])~~[sort//g]&&say' ./dict.txt acert

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2015-07-08 01:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls