Regarding the special character in Split operator

Discussion in 'Perl' started by mehul, Sep 13, 2004.

  1. mehul

    mehul Guest

    hi all,
    i am just starting with PERL and had this doubt related to the speical
    operator +.
    what it says is that in the line split( / +/,$line);
    it will not ignore the initial spaces as it starts a word when it
    encounters a space...
    so if possibly there is only a single space in the staring,then it
    shldnt be a problem isnt it?
    and also if there are more than 1 spaces then it says that also the
    word count would be 1 more than the total no of words.but what i think
    is that it will simply count the spaces in continuation with the first
    word and so the first word would have the spaces, but the count will
    remain the same.
    hope someone will clear this point for me.
    mehul.
    mehul, Sep 13, 2004
    #1
    1. Advertising

  2. mehul wrote:
    > i am just starting with PERL and had this doubt related to the
    > speical operator +.
    > what it says is that in the line split( / +/,$line);
    > it will not ignore the initial spaces as it starts a word when it
    > encounters a space...
    > so if possibly there is only a single space in the staring,then it
    > shldnt be a problem isnt it?
    > and also if there are more than 1 spaces then it says that also the
    > word count would be 1 more than the total no of words.but what i
    > think is that it will simply count the spaces in continuation with
    > the first word and so the first word would have the spaces, but the
    > count will remain the same.


    Why don't you test it to figure out how it works?

    This group does not exist. If you, after having done some testing,
    would have a question left, post it to comp.lang.perl.misc.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 13, 2004
    #2
    1. Advertising

  3. mehul

    Allan Nelson Guest

    mehul wrote:
    > hi all,
    > i am just starting with PERL and had this doubt related to the speical
    > operator +.
    > what it says is that in the line split( / +/,$line);
    > it will not ignore the initial spaces as it starts a word when it
    > encounters a space...
    > so if possibly there is only a single space in the staring,then it
    > shldnt be a problem isnt it?
    > and also if there are more than 1 spaces then it says that also the
    > word count would be 1 more than the total no of words.but what i think
    > is that it will simply count the spaces in continuation with the first
    > word and so the first word would have the spaces, but the count will
    > remain the same.
    > hope someone will clear this point for me.
    > mehul.


    Here's the output for my test:
    Count for no space was 2
    Count for one space was 3
    Count for five spaces was 3

    Here's the code that produced the output.
    #!/usr/bin/perl -w

    my $line = 'no space';
    @results = split / +/, $line;
    $count = @results;
    print "Count for no space was $count\n";

    $line = " One space";
    @results = split / +/, $line;
    $count = @results;
    print "Count for one space was $count\n";

    $line = " Five spaces";
    @results = split / +/, $line;
    $count = @results;
    print "Count for five spaces was $count\n";

    In this case the pattern for split is read as one or more spaces. By
    default in regular expressions + is greedy. So in the second case one
    leading space qualified as as a word because one space satisfied / +/
    and in the third case 5 spaces counted as a single word because + means
    one or more. I hope this code clarifies the point for you.

    Allan
    Allan Nelson, Sep 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    8,833
    Default User
    Jan 14, 2005
  2. indranil b
    Replies:
    4
    Views:
    491
    Mike Wahler
    Jan 13, 2005
  3. PerlFAQ Server
    Replies:
    0
    Views:
    378
    PerlFAQ Server
    Jan 25, 2011
  4. Ted Zlatanov

    split a special character stored in variable

    Ted Zlatanov, Apr 1, 2011, in forum: Perl Misc
    Replies:
    4
    Views:
    159
    Ted Zlatanov
    Apr 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    461
    PerlFAQ Server
    Apr 13, 2011
Loading...

Share This Page