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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |