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

hay i have a string. if i want to get a seperate word from tht how will i do that? the word means it is saperated by white space. pls help me i m beginer in perl
  • Comment on how to split string(many words in one line) in a word?

Replies are listed 'Best First'.
Re: how to split string(many words in one line) in a word?
by BrowserUk (Patriarch) on Oct 07, 2005 at 00:18 UTC

    my @words = split ' ', "The quick brown (it's a slightly reddish brown +) fox jumps over the lazy dog"; print for @words; The quick brown (it's a slightly reddish brown) fox jumps over the lazy dog

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      Except that the output generated is:

      Thequickbrown(it'saslightlyreddishbrown)foxjumpsoverthelazydog

      However this:

      print join "\n", split ' ', "The quick brown (it's a slightly reddish brown) fox jumps over the lazy dog";

      does the trick


      Perl is Huffman encoded by design.

        That is real output as generated from my "perl shell"--which has -l in force.

        The same effect can be seen by:

        P:\test>perl -wle"print for split ' ', shift" "The quick brown (it's a + slightly reddish brown) fox jumps over the lazy dog The quick brown (it's a slightly reddish brown) fox jumps over the lazy dog

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        i always start perl on the commandline with -wle. i would do that even while sleeping.
        that saves me from doing "...\n" instead of ... and -w saves me from bugs.
        i don't have an alias for that, but i'm typing it automatically. perl -e'' looks very odd to me.
        Sorry, but why split if you don't store it and join it directly? I know, it's the homework answer to split, but your's will be more efficient, I guess this way:
        s/\s+/\n/;print;

        $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
        The OP asked: "if i want to get a seperate word from tht how will i do that? ". BrowserUKs answer does what he/she asked for. The concatenation of the words is an artefact of the print output used to demonstrate that it works, rather than the operation the OP was asking about.

        --------------------------------------------------------------

        $perlquestion=~s/Can I/How do I/g;

      I think that BrowserUK might meant to say something like:

      my @words = split ' ', "The quick brown (it's a slightly reddish brown +) fox jumps over the lazy dog"; print "$_\n" for @words;

        Or, maybe he just didn't mention that you should run perl with -l on the command or shebang line. ;-)

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: how to split string(many words in one line) in a word?
by GrandFather (Saint) on Oct 07, 2005 at 01:01 UTC

    I quite like this version (though I'd not submit it as a homework answer):

    $_ = "The quick brown (it's a slightly reddish brown) fox jumps over t +he lazy dog"; split; print join "\n", @_;

    Same output as BrowserUK's code purports to give.


    Perl is Huffman encoded by design.
      tmtowtdhw :)
      my $string = "The quick brown (it's a slightly reddish brown) fox jump +s over the lazy dog"; my $words = join '', map { ( / / ) ? "\n" : $_ ; } split '', $string ; print "$words" ;
      gives Use of implicit split to @_ is deprecated at... under warnings;.


      holli, /regexed monk/

      Does this smell like the start of an obfu/golf contest? Maybe so!

      echo "The quick brown (it's a slightly reddish brown) fox jumps over t +he lazy dog" \ | perl -nle '$"="\n";print"@{[split]}"'

      Update:

      echo "The quick brown (it's a slightly reddish brown) fox jumps over t +he lazy dog" \ | perl -ple 's/\s+/\n/g'
Re: how to split string(many words in one line) in a word?
by radiantmatrix (Parson) on Oct 07, 2005 at 14:45 UTC

    Since this appears to be your first post, you might consider reading How (Not) To Ask A Question, which should help you get better answers by asking better questions.

    Also, your spelling and capitalization is atrocious. It may not matter to you, but many people will just cruise by a question with horrible spelling and grammar -- the theory being "if you don't care about your post, then you don't care about the answer". There are a number of browser plugins and the like that help with your spelling.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: how to split string(many words in one line) in a word?
by SamCG (Hermit) on Oct 07, 2005 at 14:33 UTC
    if i want to get a seperate word from tht how will i do that?
    I couldn't even interpret what this question meant.