These "snippets" will generate a "numbered sourcecode listing" in a variety of styles.

Why'd I post these, because I couldn't find any snippets (i saw all 456 of 'em), and even though it was relatively easy, someone might find it useful (since this is the 5th time I created some from scratch).

Also, there was some golf potential ;)(gotta start somewhere)

Reading material (if you don't know how it works):
perlfunc:printf,perlop,perlrun,perldata

# style: what i like (my latest first two attempts) perl -e"printf('%4s: %s',++$a,$_)while(<>)" file perl -e"printf'%4s: %s',++$a,$_ while<>" file # then i did a lot of searching, and ran accross clemburg's solution # (which was 'broken' like the craft version, see end) perl -pe "s/^/++$i.' 'x4/e" file # then i remembered -p and broke it down to ### which is by far my favorite (the one i use) perl -pe "printf'%4s: ',++$a" file # style: merlyn (as featured in WebTechniques) perl -pe"printf'%6s ','='.++$a.'='" file # or more acurately perl -pe"printf'%-6s ','='.++$a.'='" file # or most accurately (since his output looks like' =2= code +') perl -pe"printf'%8s%-8s ','','='.++$a.'='" file # style: [craft] (craft doesn't handle 3digits well) # you can always change the 2 into 3 to handle 3 digit nums perl -pe"printf'%-2s: ',++$a" file
update:
########## my favorite (never forget perlvar, thanks davorg) perl -pe "printf'%4u: ',$." file # or to prevent 'overflow' perl -pe "printf'%4.4s: ',$." file

In reply to numbered sourcecode listing (oneliners) by crazyinsomniac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.