Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Split words

by rajkrishna89 (Acolyte)
on Jul 11, 2012 at 14:37 UTC ( #981130=perlquestion: print w/ replies, xml ) Need Help??
rajkrishna89 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks , is there any way to split a word in a .txt file based on a condition ..for eg consider a line

perl monks wisdom monks perl wisdom

I want the above lines to be splitted as follows:-

perl monks wisdom monks perl wisdom

I want the words prefix or suffix of the "monks" to be printed in a new line..i tried something but i cant get the idea to implement it..Thanks Monks

Comment on Split words
Select or Download Code
Re: Split words
by cheekuperl (Monk) on Jul 11, 2012 at 14:45 UTC
    while($line=<DATA>) { if($line=~/(.*)(monks)(.*)/) { print "\n$1\n$2\n$3"; } } __DATA__ perl monks wisdom monks perl wisdom
      fails when the split word, monks, is repeated:
      __DATA__ perl monks wisdom monks perl wisdom perl perl monks monks wisdom perl perl monks monks monkly wisdom
        Nice observation!
        However, for repeating "monks" all we need is this:
        while($line=<DATA>) { while($line=~m/monks/g) { print "\n==>$`\nmonks\n$'"; } }
        Now I've assumed prefix of "monks" means whatever precedes "monks" in the string (even if it contains previous "monks") Same goes for postfix.
Re: Split words
by aitap (Deacon) on Jul 11, 2012 at 14:53 UTC
    Another way to do it: join("\n",split(/(monks)/,$string));
    Sorry if my advice was wrong.
Re: Split words
by jwkrahn (Monsignor) on Jul 11, 2012 at 14:54 UTC
    $ echo "perl monks wisdom monks perl wisdom" | perl -lne'print for split /\s*(monks)\s*/' perl monks wisdom monks perl wisdom
Re: Split words
by Utilitarian (Vicar) on Jul 11, 2012 at 14:57 UTC
    perl -e ' while(<>){@monk_split=split(/\s*(monks)\s*/);print join"\n", +@monk_split}' ~/tmp/tmp.dat perl monks wisdom monks perl wisdom
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Split words
by ww (Bishop) on Jul 11, 2012 at 15:05 UTC
    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-10-02 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (50 votes), past polls