http://www.perlmonks.org?node_id=1046481

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

i have a 2 column like this

i o am o 2 o ( o but o ) o . o how o
I need to print the column1 as a row outside the while loop so i used
push @first_sentence,$arr[0]; }print join ' ',@first_sentence;
where @first_sentence contains the 1st column .But the output is i am 2 ( but ) . how.I dont need spaces before and after brackets or dot symbol.

Replies are listed 'Best First'.
Re: remove spaces after join operation
by mtmcc (Hermit) on Jul 26, 2013 at 11:15 UTC
    Don't put a space between the quotes in the join statement:

     join('', @first_sentence);

    If you want spaces at some points but not at others, you'll either have to alter the elements going into the array, or modify your sentence after the join with spaces.

Re: remove spaces after join operation
by 2teez (Vicar) on Jul 26, 2013 at 13:54 UTC

    Hi lakssredhar,
    A suggestions:
    I need to print the column1 as a row outside the while loop so i used

    ... push @first_sentence,$arr[0]; }print join ' ',@first_sentence;
    I was kind of thinking, why, push all the column 1 into an array variable, in work on the array outside the while loop.
    what of if all work were done in the while loop? Like so:
    use warnings; use strict; my $line; while (<DATA>) { my $part = (split)[0]; if (/[.()]/) { $line =~ s/\s*$//; $line .= $part; } else { $line .= $part . q[ ]; } } print $line; __DATA__ i o am o 2 o ( o but o ) o . o how o

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: remove spaces after join operation
by AnomalousMonk (Archbishop) on Jul 26, 2013 at 14:41 UTC

    Others have noted the approach of join-ing a list of strings with a space and then taking spaces out of the string where appropriate. Conceptually, the other approach is to not put in a space in the first place. See List::Util::reduce().

    >perl -wMstrict -le "my @first_sentence = qw{ i am 2 ( but ) . }; printf qq{'$_' } for @first_sentence; print ''; ;; my $ns = qr{ [().] }xms; ;; my $sentence = join q{ }, @first_sentence; print qq{'$sentence'}; ;; $sentence =~ s{ [ ] (?= $ns) | (?<= $ns) [ ] }''xmsg; print qq{'$sentence'}; ;; ;; use List::Util qw(reduce); ;; $sentence = ''; $sentence = reduce { ($a =~ $ns || $b =~ $ns) ? qq{$a$b} : qq{$a $b} } @first_sentence ; print qq{'$sentence'} " 'i' 'am' '2' '(' 'but' ')' '.' 'i am 2 ( but ) .' 'i am 2(but).' 'i am 2(but).'

    Update: Actually, this is what 2teez already did here.

Re: remove spaces after join operation
by Loops (Curate) on Jul 26, 2013 at 11:17 UTC
    Not sure this is exactly what you're looking for, but:
    print join(' ',@first_sentence) =~ s/\s*([.()])\s*/$1/gr;
    Prints:
    i am 2(but).how

      This error Can't modify join or string in substitution (s///) at out.pl line 5, near "s/\s*(\.\(\))\s*/$1/g;" is being shown

        Ah, is your version of Perl too old to support the /r modifier? It wont work without it. I Suppose:
        my $txt = join ' ',@first_sentence; $txt =~ s/\s*([.()])\s*/$1/g; print $txt;
        Updated to simplify regex as suggested by hdb
Re: remove spaces after join operation
by Anonymous Monk on Jul 26, 2013 at 11:16 UTC