Tag: perl

Wicked Cool FizzBuzz in Perl

The FizzBuzz problem is a simple coding demonstration to write an application in any language that counts from 1 to 100:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

I found the following on Stackprinter (deleted Stackoverflow questions) by  “ysth”; probably the best example of wicked-cool code obsfucation, ever.

(                       (
''))=~('('.'?'.'{'.("`"|
'%').('['^'-').('`'|'!').
('`'|',').'"'.('['^'+').(
         ((            (
         (             ((
         (             ((
         '['         ))))
          )))))^(')')).(
           '`'|")").(
         (              (
         '`'))|'.').('['^
         '/').'+'.('(').(
         '`'^'&').(('`')|
                     ((
                      ((
                    ')'))
                   ))).+(
                   "["^

         (              (
         '!'))).('['^'!')    .')'
         .'['.''.('$').    '_'.
         '%'.('^'^(('`')|     ((
         (              (
         '-')))))).(']').
         '.'.'('.('`'^'"'
         ).('['^'.').('['
                       ^
                       ((
                       ((
         '!'))))).(('[')^
         '!').')'.('[').
         ''.'$'.'_'
                        .
            '%'.('^'^('`'|'+'
         )).']'.'|'.'|'.''.
         '$'.'_'.','.''.'$'
         .+             (
         ((
           (
                  (
                  (
                  (
                  (
            '/')))))))).(
                  (
                  (
                  (
                   '`')))|
              '&').('`'|"/").(
           '['^')').('{'^'[').('^'
         ^('`'|'/'         )).".".
         ((                      '.'

         )                         )
         .                         (
         '^'^('`'|'/')).('^'^(('`')|
         '.')).('^'^('`'|'.')).('!'^
         (              (          (
         (              (          (
                        (          (
                        (          (
                       '+'         )
                      )))))        )
                                   )
                                  ))
                               ).'"'

         .              (
         '}').')');$:='.'    ^'~'
         ;$~='@'|"(";$^=    ')'^
         '[';$/='`'|".";     $,
                     ='('
         ^+           '}'
         ;($)         =(
         ('`'))|        (
         (  "!"));     (
         (    $:))=')'  ^
         (       '}');$~=
         (          '*')|
         ((            ((
         '`')
                     )));
         $^           =((
         '+'))         ^+
         '_';$/=        (
         (  "&"))|     (
         (    '@'));$,  =
         (       '[')&'~'
         ;          ($)=
         ((            ((
         ',')

         )))^                   '|';
         $:=('.')^         "~";$~=
           '@'|'(';$^=')'^"[";$/=
               '`'|'.';$,='('

                   ^'}';$
              ='`'|'!';$:=")"^
           '}';$~='*'|'`';$^="+"^
         ('_');$/=         '&'|'@';
         $,                      =((

           (             "[")))&
          ((           ('~')));$=
         ((           ','))^    '|'
         ;           ($:)=        ((
        '.'))^'~';$~='@'|'(';$^=")"^
         (          '[');          (
         (          $/))=          (
        '`')|'.';$,='('^'}';$=('`')|
         ((        '!')            )
         ;$:=    (')')^           (
           ('}'));$~=            (

         (
         (
         (
         (
         (
         (
         (
         (

         (                     ((
         '*')                ))  ))
            )))             )      )
               ))|          (      (
                   '`'       ));$^=
           '+'       ^((
         ((   ((        '_'
         )                  )))
         )     )              ;$/
          ='&'|                  '@'

          ;$,                    =
         '['                     &+
         ((                       ((
         (                         (
         (                         (
         (             ((          (
         '~'         ))))))      )))
         )));(    ($))  =','^"|";
          $:='.'^"~";    $~="@"|
            "(";$^=

         ')'^                   '[';
         $/=('`')|         ".";$,=
           '('^'}';$='`'|"!";$:=
               ')'^'}';$~='*'

          |+
         '`';
         ($^)

                   =('+')^
              '_';$/='&'|'@';$,
           ='['&'~';$=','^'|';$:=
         '.'^"~";         $~="@"|
         ((                      '('

         )                         )
         ;                         (
         $^)=')'^'[';$/='`'|".";$,=
         '('^'}';$='`'|'!';$:=")"^
         (              (          (
         (              (          (
         (              (          (
         (              (          (
         (             '}'        ))
         ))           ))))       )))
         ));(       $~)= '*'|'`';$^
          ='+'^"_";$/=   '&'|'@';
            $,='['&'~'      ;$=
              ","^
                        (
           '|');$:=('.')^
         '~';$~='@'|"(";
         $^=')'^('[');$/=
         ((
         ((
          (
         '`')))))|'.';$,=
         '('^'}';$="`"|
         '!';$:=')'^"}";
                     ($~)
         =(           '*'
         )|'`'         ;(
         $^)='+'        ^
         (  '_');$/     =
         (    '&')|'@'  ;
         (       $,)='['&
         (          '~');
         $            =(
         ',')
                     ^'|'
         ;(           $:)
         ='.'^         ((
         "~"));        (
         (  ($~)))=     (
         (    ('@')))|  (
         (       '('));$^
         =          ")"^
         ((            ((
         '[')

         )));                   ($/)
         ='`'|'.';         $,="("^
           '}';$='`'|'!';$:=")"^
               '}';$~='*'|'`'

                   ;$^='+'
              ^'_';$/='&'|"@";
           $,='['&'~';$=','^"|";
         $:=('.')^         "~";$~=
         ((                      '@'

           )             )|'(';$^
          =(           ')')^'[';$/
         =(           "`")|    '.'
         ;           ($,)=        ((
        '('))^'}';$='`'|'!';$:=")"^
         (          '}');          (
         (          $~))=          (
        '*')|'`';$^='+'^'_';$/=('&')|
         ((        '@')            )
         ;$,=    ('[')&           (
           ('~'));$=            (

         (
         (
         (
         (
         (
         (
         (
         (

         (                     ((
         ',')                ))  ))
            )))             )      )
               ))^          (      (
                   '|'       ));$:=
           '.'       ^((
         ((   ((        '~'
         )                  )))
         )     )              ;$~
          ='@'|                  '('

          ;(           ($^))=
         ((             (  (')'))))^
         (              ((      '[')
         )              );      ($/)
         =              ((      '`')
         )|            '.'      ;$,=
         "("^      '}';$      ='`'
          |'!';$:=')'^'}'       ;$~=
            '*'|'`';$^=         '+'^

         '_';                   ($/)
         ='&'|'@';         $,="["&
           '~';$=','^'|';$:="."^
               '~';$~='@'|'('

          ;(               ($^))=
         ')'^   '[';$/='`'|('.');$,=
         '('^      '}';$='`'|"!";

          $:               =")"^
         '}';   $~='*'|'`';$^=('+')^
         '_';      $/='&'|('@');$,=

           (             '[')&'~'
          ;(           $)=','^'|'
         ;(           ($:))=    '.'
         ^           "~";        $~
        ='@'|'(';$^=')'^'[';$/=('`')|
         (          '.');          (
         (          $,))=          (
        '(')^'}';$='`'|'!';$:=(')')^
         ((        '}')            )
         ;$~=    ('*')|           (
           ('`'));$^=            (

         (
         (
         (
         (
         (
         (
         (
         (

          ((
         '+')
       ))))))

           )             )))^'_';
          $/           ='&'|'@';$,
         =(           "[")&    '~'
         ;           ($)=        ((
        ','))^'|';$:='.'^'~';$~="@"|
         (          '(');          (
         (          $^))=          (
        ')')^'[';$/='`'|'.';$,=('(')^
         ((        '}')            )
         ;$=    ('`')|           (
           ('!'));$:=            (

         (
         ')')
            )^+
               '}'
                   ;$~
                     =((
                        '*'
                            ))|
                              '`'
                                 ;$^
                        =
         '+'^'_';$/='&'|"@";$,=
         '['&'~';$=','^'|';$:='.'^
         '~';$~='@'|'(';$^=')'^"[";
                        (      $/  )
                        =     ('`')|
                              ".";
              ($,)=
           '('^'}';$=
          '`'|'!';$:=')'
         ^+           ((
         (              (
         (              (
         (             ((
         '}')       ))))
           ))));$~='*'|
             "`";$^=
         (              (
         '+'))^'_';$/='&'
         |'@';$,='['&'~';
         $=','^('|');$:=
                     ((
                      ((
                    '.'))
                   ))^'~'
                   ;($~)

         =
         (                    (
         '@'))|'(';$^=')'^'[';$/=
         '`'|'.';$,='('^'}';$='`'
         |
         (
          ((
         '!')
         ));(

          $:
         )=((
         ')')

         )
         ^                    (
         '}');$~='*'|'`';$^="+"^
         '_';$/='&'|'@';$,='['&'~'
         ;
         (
                  $)=","^
             '|';$:='.'^'~';$~=
           '@'|'(';$^=')'^'[';$/=
          "`"|               ".";
         $,                       =(
         (                         (
         (                         (
         ((                      '('
          )))))               ))^((
           '}'));$='`'|('!');$:=
              ')'^'}';$~="*"|

                  ('`');$^=
             '+'^'_';$/='&'|'@'
           ;$,='['&'~';$=','^'|'
          ;($:)               ='.'^
         ((                       ((
         (                         (
         (                         (
         ((                      '~'
          )))))               )))))
           ;$~='@'|'(';$^=')'^'['
              ;$/='`'|".";#;#

Save the above Asciiart into a file called, for example, fizz.pl.

Then run it: perl fizz.pl

Enhanced by Zemanta

Bulk fix a list of words to the proper case

A set of metal types
A set of metal types (Photo credit: Wikipedia)

Problem

Given a long list of upper-case words (one word per line), such as:

MARY
JOE
SCOTT
FRED
... (for thousands of rows) ...

You want to change this to

Mary
Joe
Scott
Fred
(etc)

Solution

Perl
Perl (Photo credit: Wikipedia)

Use perl from the command line. Assume all your names are in a file called ‘names.txt’. We’re going to send the output to a new file called ‘cap_names.txt’.

$ perl -pe 's/^(.)(.*)$/1L2/g' names.txt > cap_names.txt

How this works

On the matching side:
^(.) → get the first character. The parenthesis allow us to refer to it as 1.

(.*)$ → get the rest of the line. The parenthesis allow us to refer to it as 2.

On the substitution side:

1 → outputs that first character, as is.

L2 → outputs everything else, lower case (the L makes everything in 2 lower case).

Neat!

Inspired by Regexident’s answer at StackOverflow.

Enhanced by Zemanta