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

I want to pratice in perlonelines. I've learned -e -E -M -n -a -F and now I want to practice in writing oneliners. Can you please give some some tasks (without answers) that are good to be solved by oneliners?
  • Comment on Learing perl oneliners. Can you give me some problems to solve.

Replies are listed 'Best First'.
Re: Learing perl oneliners. Can you give me some problems to solve.
by Laurent_R (Canon) on Oct 02, 2014 at 17:29 UTC
    You might also want to learn about the -p and -i command line flags. They are also very useful.

    A few easy things you could try:

    - Removing DOS carriage returns from a file under Unix (a sort of dos2unix utility)

    - Renaming in uppercase (or lowercase) all the files of a directory, checking first that the uppercase (or lowercase) version does not already exists.

    - Removing all comments from a Perl program

    - Crypting a file or a string in rot13 (each letter shifted by 13 positions in the alphabet), and decrypting it.

    - Listing all the modules installed in your Perl environment

    - Listing all the files in a directory, sorted by name / size / age or whatever other property you might want.

    - Changing the date format in a text file (for example from MM DD, YYYY to YYYY-MM-DD or vice-versa.

    - Printing the multiplication tables for numbers between 1 and 10.

Re: Learing perl oneliners. Can you give me some problems to solve.
by blindluke (Hermit) on Oct 03, 2014 at 08:32 UTC

    I could add:

    • Printing out all lines containing TODO/FIXME in a given file

    There is also a fairly recent book on the subject: Perl One Liners. And Perl White Magic is a wonderful thing to read.

    regards,
    Luke Jefferson

Re: LearNing perl oneliNErs. 4 more problems.
by Discipulus (Canon) on Oct 03, 2014 at 08:22 UTC
    you may also try these:

    • given a glob (like /var/log/accesslog*) as argument print a descendant list of distinct occurences of the nth field, like IPs (BEGIN is your friend)
    • given a filename print only the content between two words
    • realize a mini calculator using the native math ability of Perl, print errors too
    • format a given file to have line of n chars

    have fun!
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Learing perl oneliners. Can you give me some problems to solve.
by karlgoethebier (Abbot) on Oct 03, 2014 at 18:10 UTC
Re: Learing perl oneliners. Can you give me some problems to solve.
by Anonymous Monk on Oct 02, 2014 at 23:19 UTC

    Your first task is, come up with a list of tasks that could be solved with oneliners

    :)