Re: [OT] My First C# (warning - long post)
- From: LX-i <lxi0007@xxxxxxxxxxxx>
- Date: Thu, 01 Feb 2007 19:12:32 -0600
Frank Swarbrick wrote:
LX-i<lxi0007@xxxxxxxxxxxx> 02/01/07 7:51 AM >>>Along those lines - is there a better way to search through an array other than "for (int i = 0; i < array.length; i++)"?
I'm not sure specifically what you are referring to, but, if we assume the
above code is something like
string array[] = new string[10];
for (int i = 0; i < array.length; i++)
{
doSomething(array[i]);
}
Not quite... but close enough.
You can replace it with
foreach (string s in array)
{
doSomething(s);
}
Is that what you're looking for?
Sort of. I've been doing some digging, and had found foreach() - I also found the DataSet's ReadXml method. This is what I've got at the current time...
protected DataSet keywords = new DataSet();
....
// This is executed in the constructor
protected void fillCobolKeywords() {
keywords.ReadXml("cobol_keywords.xml");
}
....
// Returns "true" if the word passed is a COBOL keyword
protected bool isCobolKeyword(String ickText)
{
bool ick = false;
DataTable dt = keywords.Tables[0];
foreach (DataRow dr in dt.Rows)
{
if (dr[0].ToString() == ickText)
{
ick = true;
}
}
return ick;
}
With this, though, if the keyword is "ACCEPT", it's still plowing through 350+ entries before returning. Could (should) I short-circuit it and return true in the loop - something like this...
// Returns "true" if the word passed is a COBOL keyword
protected bool isCobolKeyword(String ickText)
{
DataTable dt = keywords.Tables[0];
foreach (DataRow dr in dt.Rows)
{
if (dr[0].ToString() == ickText)
{
return true;
}
}
return false;
}
I appreciate the suggestion. foreach is pretty cool - I've used it in PHP quite a bit. I guess what I was looking for was an array.search method or something that may apply a more efficient searching algorithm than just "gimme one - is this it? nope, gimme another one - is this it? nope..." etc.
(The only reason I'm concerned with this sort of efficiency is that this ....er... whatever it is (class/component/process masquerading as OO) will run as part of a page post from our .NET CM site. If I'm rebuilding the indexes, it doesn't matter if it takes 20 seconds per program - but in that web page, it would be unacceptable.)
Hmm.... Maybe this needs to be a separate thread... Can an ASP.NET page spawn a thread that runs after the response has been sent to the user? Sounds like I've got more digging to do.... I wish I wasn't moving in 2 weeks! It seems like there's just too much to consider - I guess there's got to be something for the person who comes after me to do. :)
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ / \ / ~ Live from Montgomery, AL! ~
~ / \/ o ~ ~
~ / /\ - | ~ daniel@thebelowdomain ~
~ _____ / \ | ~ http://www.djs-consulting.com ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ GEEKCODE 3.12 GCS/IT d s-:+ a C++ L++ E--- W++ N++ o? K- w$ ~
~ !O M-- V PS+ PE++ Y? !PGP t+ 5? X+ R* tv b+ DI++ D+ G- e ~
~ h---- r+++ z++++ ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Who is more irrational? A man who believes in a God he doesn't see, or a man who's offended by a God he doesn't believe in?" - Brad Stine
.
- References:
- Re: [OT] My First C# (warning - long post)
- From: LX-i
- Re: [OT] My First C# (warning - long post)
- From: Pete Dashwood
- Re: [OT] My First C# (warning - long post)
- From: LX-i
- Re: [OT] My First C# (warning - long post)
- From: Frank Swarbrick
- Re: [OT] My First C# (warning - long post)
- Prev by Date: Re: My First C# (warning - long post)
- Next by Date: Re: My First C# (warning - long post)
- Previous by thread: Re: My First C# (warning - long post)
- Next by thread: Re: [OT] My First C# (warning - long post)
- Index(es):