473,378 Members | 1,444 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.

For statement

Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.
It doesn't appear to increment n at all, nor does it appear to ever evaluate
the 3rd statement of the for loop. The string I debugged with has 3 newlines
at various locations.

Thanks,
Gary
Nov 16 '05 #1
8 1246
> Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.


The for statement has the following form:
for ([initializers]; [expression]; [iterators];) {
// statements
}

The initializers are a comma separated list of statements or expressions to
initialize counters.
The expression is an expression that can be evaluated as a boolean value. If
the expression evaluates to true the statements inside the for are executed
and then the iterators are evaluated.
The iterators are statements to increase or decrement the counters.

In your code the expression at==-1 always evaluates to false since at is
initialized to 0.

To count the number of occurances of a new line you can use this code:
int startPos=0;
int foundPos=0;
int count=0;
do {
foundPos=msg.IndexOf('\n',startPos);
if (foundPos>-1) {
startPos=foundPos+1;
count++;
}
} while(foundPos > -1 && startPos < msg.Length);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #2
>Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

^^

I believe you want != there.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
> >Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

^^

I believe you want != there.

This won't work unless n is initialized to -1 because it will do a iteration
for at==-1 resulting in n being of by one. Of course, you could subtract 1
from n after the loop. Still, it would be faster than my example.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #4

"Mattias Sjögren" <ma********************@mvps.org> a écrit dans le message
de news: eR**************@TK2MSFTNGP15.phx.gbl...
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++; ^^

I believe you want != there.


Even with at != 1, it does not work. If first char is \n, the loop won't see
it because IndexOf is called with an index of 1 the first time.
And also, the count is incremented once more than necessary.

The following should work:

int n = 0;
for (int at = -1; (at = msg.IndexOf('\n', at + 1)) != -1; )
n++;

but I would prefer:

int n = 0;
int at = -1;
while ((at = msg.IndexOf('\n', at + 1)) != -1)
n++;

Unfortunately, the while syntax does not let me scope the at variable, but
this looks cleaner.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #5
=?Utf-8?B?R2FyeUZl?= <Ga****@discussions.microsoft.com> wrote in
news:03**********************************@microsof t.com:
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a
string. When I step through it in the debugger, it highlights
the first statement (int at=0),
then highlights the second statement, then jumps to the next
line of code. It doesn't appear to increment n at all, nor does
it appear to ever evaluate the 3rd statement of the for loop.
The string I debugged with has 3 newlines at various locations.


Gary,

In addition to the other posts, here is a non-deterministic way to
count the number of newlines in a string:

using System.Text.RegularExpressions;
...
string input = "one\ntwo\nthree\n";
string regex = "\n";
int numberOfMatches = Regex.Matches(input, regex, RegexOptions.Singleline).Count;

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #6
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
news:Xn**********************************@207.46.2 48.16:
In addition to the other posts, here is a non-deterministic way
to count the number of newlines in a string:


Duh! I really should proofread before I post. I meant non-
procedural, not non-deterministic.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #7
Whatever! The reply was useful, and I thank you.

Gary

"Chris R. Timmons" wrote:
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
news:Xn**********************************@207.46.2 48.16:
In addition to the other posts, here is a non-deterministic way
to count the number of newlines in a string:


Duh! I really should proofread before I post. I meant non-
procedural, not non-deterministic.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #8
Yoiks! My misunderstanding! I believe your correction will fix me right up.

Gary

"Mattias Sjögren" wrote:
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

^^

I believe you want != there.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #9

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

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
23
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.