Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Split words

by ww (Bishop)
on Jul 11, 2012 at 15:05 UTC ( #981147=note: print w/ replies, xml ) Need Help??


in reply to Split words

Is your intention to create a new line only upon the appearance of the word "monks?" If not, please refine your spec.

If so, split is your friend. As split explains (recommended reading!), the function offers the option to -- relevant here -- retain the term upon which you're splitting.

Here's one long-winded (and not quite perfect) approach:

#!/usr/bin/perl use 5.014; my @arr = ( "line1 perl monks wisdom", "line2 monks perl wisdom", "line3 perl perl monks monks wisdom", "line4 perl perl monks monks monkly wisdom", ); my $matchword = qr/monks/; for my $line (@arr) { if ( $line =~ $matchword ) { my @arr_out = split /($matchword)/, $line; for my $arr_element(@arr_out) { say $arr_element; } } } =head OUTPUT line1 perl monks wisdom line2 monks perl wisdom line3 perl perl monks monks wisdom line4 perl perl monks monks monkly wisdom =cut


Comment on Re: Split words
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-27 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (176 votes), past polls