Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Splitting a Sentence

by Tux (Monsignor)
on Jul 03, 2014 at 10:56 UTC ( #1092145=note: print w/ replies, xml ) Need Help??


in reply to Splitting a Sentence

substitue the newlines with spaces, otherwise

this is line one rous is the first word of line two

will become

this is line onerous is the first word of line two

which is not what you want

open my $fh, "<", "test" or die "test: $!" while (<$fh>) { chomp; $s .= " $_"; }

or shorter

my $s = join " " => split m/\n/ => do { local (@ARGV, $/) = "test"; <> };

Or even shorter

my ($s = do { local (@ARGV, $/) = "test"; <> }) =~ s/\n+/ /g;

Enjoy, Have FUN! H.Merijn


Comment on Re: Splitting a Sentence
Select or Download Code
Re^2: Splitting a Sentence
by johngg (Abbot) on Jul 03, 2014 at 11:29 UTC

    TIMTOWTDI - chomp in a map and join seems fairly concise yet readable.

    $ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; Line 1 Line 2 Line 3 EOD my $wholeText = join q{ }, map { chomp; $_ } <$inFH>; say $wholeText;' Line 1 Line 2 Line 3 $

    I hope this is of interest.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1092145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-12-23 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls