Re: cocoa-dev digest, Vol 1 #351 - 16 msgs
Re: cocoa-dev digest, Vol 1 #351 - 16 msgs
- Subject: Re: cocoa-dev digest, Vol 1 #351 - 16 msgs
- From: Tony Donlon <email@hidden>
- Date: Fri, 3 Aug 2001 00:16:32 -0500
On Thursday, August 2, 2001, at 08:10 , Chris Gehlker wrote:
On 8/2/01 8:28 AM, "Mark Wridt" <email@hidden> wrote:
Hello all,
I am new to the list, and to Cocoa(I do have a C++ and
Java background). I am in the process of attempting
to read in a few tab dilimited files, parse them and
then write an ouput file with selected data. Obj-C is
most likely the issue here for me, and I was
wondering if anyone could help (with example code, or
perhaps some URL's)?
I think you'll have the best luck using straight C to read in the
files.
If
the output file is also supposed to be tab delimited, you probably had
best
use straight C to write it out as well.
I disagree. Using NSString's componentsSeparatedByString:, you can
simplify the task to a few lines:
NSArray *data=[line componentsSeparatedByString:@"\t"];
Now you have an array of strings ("line" is one line of that text file).
andy
--
Discussion forthcoming.
Message: 11
Date: Thu, 2 Aug 2001 22:57:14 +0200
From: Nat! <email@hidden>
To: email@hidden
Subject: Re: file parsing
Am Donnerstag, 2. August 2001 um 20:10 schrieb Chris Gehlker:
On 8/2/01 8:28 AM, "Mark Wridt" <email@hidden> wrote:
Hello all,
I am new to the list, and to Cocoa(I do have a C++ and
Java background). I am in the process of attempting
to read in a few tab dilimited files, parse them and
then write an ouput file with selected data. Obj-C is
most likely the issue here for me, and I was
wondering if anyone could help (with example code, or
perhaps some URL's)?
This is a simple minded approach for parsing
NSString *s;
NSArray *lines;
NSArray *fields;
unsigned int i, n;
// read complete file into a NSString
s = [NSString stringWithContentsOfFile:@"datafile.txt"];
// separate lines by linefeed into an array of lines
lines = [NSString componentsSeparatedByString:@"\n"];
n = [lines count];
for( i = 0; i < n; i++)
{
// separate each line again by TAB into fields, and output them
fields = [[line objectAtIndex:i] componentsSeparatedByString:@"\t"];
NSLog( @"Parsed fields: %@", fields);
}
I have no idea, how to write the selected data to a file though... :)
Nat!
Hey folks, this is *nix ...use Perl for file parsing!
#! /usr/bin/perl
#read each file passed and act on default variable (called "$_" but Perl
assumes you are operating on this variable).
while (<>)
{
#put each delimited value into an array
@parts = split(/\,/);
#do something with each element of the array
foreach $word (@parts)
{
doSomethingWith($word);
}
}
yeah I know this list is for cocoa but just wanted to mention the
obvious solution in a *nix environment.
:)
TonyD