I am looking for a "smooth" way to modify the code I'm simulating below... I read in a file "line-by-line" to an array and split on newline, leaving just the value of the line without the \n ... So I can create a delimited line of several items... first example
Where I get in trouble is where a line read in is blank, which shifts everything to the left in the output (notice the rogue ; at the end) because splitting on the newline leaves an empty entry... second example
I can check for an empty line and put a space in there which works, but it's just not sexy... third example

$ perl -e 'BEGIN{@words = ("one\n","two\n","three\n","four\n","five\n" +,"six\n",)} $a=0;printf("%s;%s;%s;%s;%s;%s\n",(split/\n/,@words[$a]), +(split/\n/,@words[$a+1]),(split/\n/,@words[$a+2]),(split/\n/,@words[$ +a+3]),(split/\n/,@words[$a+4]),(split/\n/,@words[$a+5]) );' one;two;three;four;five;six $ perl -e 'BEGIN{@words = ("one\n","two\n","three\n","\n","five\n","si +x\n",)} $a=0;printf("%s;%s;%s;%s;%s;%s\n",(split/\n/,@words[$a]),(spl +it/\n/,@words[$a+1]),(split/\n/,@words[$a+2]),(split/\n/,@words[$a+3] +),(split/\n/,@words[$a+4]),(split/\n/,@words[$a+5]) );' one;two;three;five;six; perl -e 'BEGIN{@words = ("one\n","two\n","three\n"," \n","five\n","six +\n",)} $a=0;printf("%s;%s;%s;%s;%s;%s\n",(split/\n/,@words[$a]),(spli +t/\n/,@words[$a+1]),(split/\n/,@words[$a+2]),(split/\n/,@words[$a+3]) +,(split/\n/,@words[$a+4]),(split/\n/,@words[$a+5]) );' one;two;three; ;five;six

I've been messing with this for 2 days now, and I'm wondering if anyone has a different way of thinking to share?

tia

In reply to split on newline by tbone654

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.