Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSScanner Blues

NSScanner Blues
Thread Tools
DaGuy
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jul 10, 2003, 07:16 PM
 
Before I write a bunch of code, I must ask if there's a straightforward way to do the following data load.

I have a flat file which is comma delimited and has a CR for end of line. Pretty standard.

I would like to use NSScanner to figure out how many columns are present in a row. This can be done by simply counting the commas until the CR is encountered.

This is the first time that I use NSScanner and I haven't found anything half decent on it. I could do this by some brute force approach but I'll float this around and see what neat suggestions I get.

Thanks in advance.

     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 10, 2003, 08:08 PM
 
This seems to do the trick - you might be able to find a faster/easier/better way, but this is what I came up w/:

- (int)numberOfColumns
{
int numberOfColumns;
NSString *firstLine;
NSScanner *scanner = [[NSScanner alloc] initWithString:@"this,is,a,test,of,the,emergency,b roadcast,system\nthis,is,only,a,test."];
if ( [scanner scanUpToString:@"\n" intoString:&firstLine] )
numberOfColumns = [[firstLine componentsSeparatedByString:@","] count];
else
numberOfColumns = -1; // error
return numberOfColumns;
}
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jul 10, 2003, 11:22 PM
 
Yup that looks a whole lot more straightforward then what I was thinking. Appreciate your assistance, thanks very much.

     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 10, 2003, 11:32 PM
 
Glad I could help
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 01:59 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,