473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Regarding the special character in Split operator

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.
Jul 19 '05 #1
2 8031
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
Jul 19 '05 #2
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dont bother | last post by:
Hi Buddies, I am facing this problem and I dont know what to use as EOF in python: I want to read a file, and put all the individual words in a dictionary with their index: For example if the...
15
by: zealotcat | last post by:
Hi, all: I want to implement a function (using std::string and std::vector) split a string contain special token into vector. eg: string = "alex delia john" ==> vector = "alex"; vector =...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
7
by: Tom de Neef | last post by:
I need to change one character at a known position in a string. In Pascal I would change the P's character of a string S into 'x' with S:='x'; In JavaScript I come no further than S =...
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: zaphod42 | last post by:
I've been crunching on this all afternoon: I need to stop text from wrapping at a special character....if you set the innerHTML of some given element without setting the width of said element, it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.