× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Steve Richter wrote:
...
The yield return op code is one of my favorites in its neatness ( and,
I think, is unique to C# ). Here is an extension method which returns
an IEnumerable interface to a sequence of Int which is parsed from a
string of comma separated numbers:

public static IEnumerable<int?> ParseLine(this string InLine)
{
string[] splitItems = InLine.Split(',');
foreach (string item in splitItems)
{
int? itemIntValue = item.DefaultParse();
yield return itemIntValue;
}
}

// DefaultParse is a method that extends the string class.
// It returns the integer value parsed from a string.
// If the string cant be parsed, it returns a null.
// The ? after int? makes the int a nullable type.
public static int? DefaultParse(this string InText)
{
int answer;
bool rv = int.TryParse(InText, out answer);
if (rv == true)
return answer;
else
return default(int?);
}

to use the method:
string textLine = "123,213,33,3534" ;
foreach( int? item in textLine.ParseLine( ))
{
if ( item == null )
MessageBox.Show("item could not be parsed") ;
else
MessageBox.Show( item.Value ) ;
}

Steve: The yield statement goes back a long time. I couldn't tell you
when a programming language first had that kind of feature, but C#
definitely was not the first. Another commonly used modern language with
yield is Python. But in Python, I wouldn't bother using yield for that
kind of thing. To convert a comma separated string of numbers into a
list, I'd code something like this:

-----------
line = "123,213,33,3534"
numbers = [int(n) for n in line.split(",")]
print numbers
-----------

To be fair, I'm taking a shortcut in assuming that each number is valid.
Here's some more robust code:

-----------
line = "123,213,33,3534"
numbers = [int(n) if n.isdigit() else None for n in line.split(",")]
print numbers
-----------

You want to see how to code this using a yield in Python? OK:

-----------
def getNum(line):
for n in line.split(","):
yield int(n) if n.isdigit() else None

line = "123,213,33,3534"
for num in getNum(line):
if num:
print num
else:
print "item could not be parsed"
-----------

Frankly, I think Python has spoiled me. I look at C# code and I see
ugliness and cruft.

Cheers! Hans


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.