use Modern::Perl qw/2014/; { local $/ = ' '; while () { chomp; if (/^0\n*$/) { say "0 - End of line"; next; } elsif (/^0\n(\d+)$/) { say "0 - End of line"; say ">$1<"; next; } else { say ">$_<"; } } } __DATA__ 1 34 282716 7 20 333333 91 0 23 68 82629172 112 8271718 102 1 0 7 211 2 123 0 99 666 0 #### >1< >34< >282716< >7< >20< >333333< >91< 0 - End of line >23< >68< >82629172< >112< >8271718< >102< >1< 0 - End of line >7< >211< >2< >123< 0 - End of line >99< >666< 0 - End of line