tbone654 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: split on newline
by ikegami (Patriarch) on Sep 12, 2014 at 19:06 UTC
    my @words = <>; chomp(@words); print(join(';', @words), "\n");

    As a one-liner:

    perl -nle'push @w, $_; END { print join ";", @w }'

    -l inserts a chomp; into -n's (and -p's) loop.

      That works... the -l is the secret, thank you... I was chomp'ing it manually yesterday and it was blowing up...
        It's not the chomping that was blowing up your code.
        perl -nle' push @w, $_; END { print join ";", @w } '
        is the same as
        perl -ne' BEGIN { $\ = "\n"; } chomp; push @w, $_; END { print join ";", @w } '

        You had some other problem yesterday. chomp-ing 'manually' won't blow anything up.

Re: split on newline
by Laurent_R (Canon) on Sep 12, 2014 at 21:32 UTC
    Although ikegami has provided a much shorter and better solution, I would like to come back to your very complicated syntax and suggest some possible improvements to the rest of your code. Using chomp, you could do this:
    $ perl -e '@words = ("one\n","two\n","three\n","four\n","five\n","six\ +n",); chomp @words; print join";", @words;' one;two;three;four;five;six
    Although I would not really recommend this, you could also use split if you really wanted to:
    $ perl -e '@words = ("one\n","two\n","three\n","four\n","five\n","six\ +n",); @words = map {split /\n/, $_ } @words; print join";", @words;' one;two;three;four;five;six
    Or perhaps a regex:
    $ perl -e '@words = ("one\n","two\n","three\n","four\n","five\n","six\ +n",); @words = map {s/\n//; $_} @words; print join";", @words;' one;two;three;four;five;six
    Or split again, but without a map this time:
    $ perl -e '@words = ("one\n","two\n","three\n","four\n","five\n","six\ +n",); $_ = (split /\n/)[0] for @words; print join";", @words;' one;two;three;four;five;six
    Or, if you don't want to use the join function:
    $ perl -E '@words = ("one\n","two\n","three\n","four\n","five\n","six\ +n",); local $" = ";"; $_ = (split /\n/)[0] for @words; say "@words";' one;two;three;four;five;six
    Well, I could probably offer at least a dozen other solutions mixing the various ideas above or introducing new ones, but I hope you get the idea of how to do the things in a much more concise way than what you had.

      You could save yourself some typing when initialising @words by using a HEREDOC and split with a look-behind.

      $ perl -Mstrict -Mwarnings -E ' my @words = split m{(?<=\n)}, <<EOL; one two three EOL say qq{$_-----} for @words;' one ----- two ----- three ----- $

      So, taking the whole thing together.

      $ perl -Mstrict -Mwarnings -E ' say join q{;}, map { chomp; $_ } split m{(?<=\n)}, <<EOL; one two three EOL' one;two;three $

      I hope this is of interest.

      Update: Removed the $PS2 prompts fromthe second example for easier copy'n'paste.

      Cheers,

      JohnGG