Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Regarding the special character in Split operator

Reply
Thread Tools

Regarding the special character in Split operator

 
 
mehul
Guest
Posts: n/a
 
      09-13-2004
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.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-13-2004
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
 
Reply With Quote
 
 
 
 
Allan Nelson
Guest
Posts: n/a
 
      09-19-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 04-13-2011 10:00 PM
split a special character stored in variable Ted Zlatanov Perl Misc 4 04-06-2011 01:54 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments