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

Dear Monks,
I have some oneline-snippets that I have written, I was wondering if they can be presented as-is or I must use also the -n and -p operators...My problem is that I don't know how I can insert these operators in the game!
All the code I am posting below works, it's just that, because I don't really know much regarding oneliners, maybe I can write them more "sophisticated". I don't want to change the code itself, I am rather trying to understand how I can put -n and -p in place...
Example1: perl -e 'while(<>) {if($_=~/^>/) {print $_;} else {$_=~tr/ATCG/TAGC/; +print $_;}}' FILE Example2: perl -e 'while(<>) {if($_=~/^P00520/) {print $_;}}' FILE Example3: perl -e '$count=0; while(<>) {$count++; if($count<=250) {print $_;}}' +FILE Example4: perl -e '$count=0; while(<>) {if($_=~/^>/) {$count++; print ">$count\n +";} else {print $_;}}' FILE Example 5: perl -e 'while(<>) {my $line=$_; if($line!~/^\s+/) {chomp $line; my @s +plit_line=split(/\s+/, $_); my $wanted_pos=$#split_line-1; print "$sp +lit_line[0]\t$split_line[$wanted_pos]\n";}}' FILE Example6: perl -e 'my $count_serines=0; my $count_positives=0; while(<>) {my $li +ne=$_; $count_serines++; if($line!~/^\s+/) {chomp $line; my @split_li +ne=split(/\s+/, $_); my $wanted_pos=$#split_line-1; if($split_line[$w +anted_pos] eq "*S*") {$count_positives++;}}} print "TOTAL_SERINES: ". +$count_serines."\t"."POSITIVE_PREDICTIONTS: ".$count_positives."\n";' + FILE

Replies are listed 'Best First'.
Re: Can you help me with these one-liners?
by BrowserUk (Patriarch) on Apr 22, 2014 at 15:20 UTC
    Example1: perl -e 'while(<>) {if($_=~/^>/) {print $_;} else {$_=~tr/ATCG/TAGC/; +print $_;}}' FILE

    Use -n to replace the while loop:

    perl -nle 'if($_=~/^>/) {print $_;} else {$_=~tr/ATCG/TAGC/; print $_; +}' FILE

    Do away with the unnecessary explicit references to $_:

    perl -nle 'if(/^>/) {print;} else { tr/ATCG/TAGC/; print; }' FILE

    Recognise that every line is being printed, so -p can replace the explicit print statements:

    perl -ple 'if(/^>/) { ; } else { tr/ATCG/TAGC/; }' FILE

    Rearrange to do away with the empty if body:

    perl -ple 'tr/ATCG/TAGC/ unless( /^>/ );' FILE

    UPDATE: a final step:

    perl -ple '/^>/ or tr/ATCG/TAGC/' FILE

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Can you help me with these one-liners?
by Corion (Patriarch) on Apr 22, 2014 at 14:31 UTC

    If you have written them, maybe you want to (re)visit perlrun to see where and how -n and -p come into play?

    If you don't tell us why you want to change your existing programs to use -n or -p, we can't really advise where you would need them.

Re: Can you help me with these one-liners?
by 2teez (Vicar) on Apr 22, 2014 at 14:49 UTC

    .. maybe I can write them more "sophisticated"..

    If you use a 'code' more than a couple of times, why keep it as a one liner? Why not 'pump' it up into a 'fully fledged' scripts, maybe even useful to some other persons around you? Just thinking aloud.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      No no, sorry maybe I should have said it before... These are supposed to be my answers to small snippets of code that we must write for a course. And the teacher asked us to write them in Perl oneliners (this is the topic for the current lesson). I was just reading up the -n and -p functions, but in my little experience with Perl oneliners, I don't really understand how I could modify these lines and use the -n and -p (or if I SHOULD actually modify them or just let them as they are, since they work in my tasks).