Here's how I might have done that. I've accounted for elements with embedded and terminal newlines. This also prints a final newline if the last element didn't end with one.

#!/usr/bin/env perl use strict; use warnings; my $delimiter = '<TAB>'; my @info = (1 .. 5, "\n", 6 .. 10, "\n", "A\nB", "C\n", 'D', 'end'); print /\n$/ ? $_ : "$_$delimiter" for @info[0 .. $#info - 1]; print $info[-1] =~ /\n$/ ? $info[-1] : "$info[-1]\n";

Output:

1<TAB>2<TAB>3<TAB>4<TAB>5<TAB> 6<TAB>7<TAB>8<TAB>9<TAB>10<TAB> A B<TAB>C D<TAB>end

[Obviously, <TAB> is just used as a visual aid: change '<TAB>' to "\t" in the real application.]

Update: Changed "'<TAB>' to "\n"" to "'<TAB>' to "\t"". Thanks to Laurent_R for spotting that.

-- Ken


In reply to Re: Splicing the next element in an array by kcott
in thread Splicing the next element in an array by Anonymous Monk

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.