Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: command line perl reading from STDIN

by choroba (Abbot)
on Jan 22, 2013 at 17:32 UTC ( #1014741=note: print w/ replies, xml ) Need Help??


in reply to command line perl reading from STDIN

<> is a nicer name for readline. In scalar context, which is the case of your code, it reads one line from the input. If you do not assign the returned value to a variable, the line is skipped.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: command line perl reading from STDIN
Download Code
Re^2: command line perl reading from STDIN
by perlhappy (Novice) on Jan 22, 2013 at 17:54 UTC
    cat input_file.fq | perl -ne '$s=<>;<>;<>;chomp($s);print length($s)."\n";' > output.txt
    Ok, but for the above, if we were to step-by-step describe what is going on. How would it be described? It just that from the above I thought it would assign $s on every line OR skip 2 lines and then assign $s to the third line and repeat.
        At the start, its the second line and then every fourth file after. ie line 2, 6, 8, 12 .. eof
        This is what was confusing me but I think I worked it out. See below. Thanks
      Actually, I think I've just worked it out my head. Took a bit of thinking but this is what i think it is doing
      first line gets sent, but because <> is unassigned (before $s), it skips; then reads the second line and assigns $s to the line and does whatever; then reads third line but because <> is unassigned, it skips; reads fourth line and the same happens; restarts with 5 line but again because <> is unassigned before the $s it skips; reads 6th line and assigns it to $s and does whatever again
      this continues until end of file

      If this is incorrect, let me know. Otherwise I hope this helps anyone else who might look at it. Thanks for your help choroba
         perl -ne ' $s=<>; <>; <>; do_something_with_$s'

        is equivalent to

         perl  -e ' while (<>) { $s=<>; <>; <>; do_something_with_$s} '

        so 4 lines will be read each pass, but only the second line will be used for anything

        Personally, I'd go with something like

         perl -ne 'chomp; print length($_)."\n" if (/^@/);'

        instead.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-09-20 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (152 votes), past polls