Re: How to reference field in record of TStringList
Von: Rob Kennedy (me3@privacy.net) [Profil]
Datum: 05.10.2008 00:09
Message-ID: <6kq7toF95ic3U1@mid.individual.net>
Newsgroup: alt.comp.lang.borland-delphi
Datum: 05.10.2008 00:09
Message-ID: <6kq7toF95ic3U1@mid.individual.net>
Newsgroup: alt.comp.lang.borland-delphi
Chris.Cheney wrote: > Rob Kennedy <me3@privacy.net> wrote in news:6koidsF8vs8aU1 > @mid.individual.net: > >> TD wrote: >>> How do you reference a particular field in a record of a csv file >>> using TStringList, like the second field in the third string? >>> >>> Example - TStringList[3,2] maybe? >> That obviously doesn't work. You tried it, right? >> >> What would that mean for lists of strings? That's what a TStringList > is, >> remember. It's not a list of CSV records. It's not a list of anything >> with any meaning, in fact. >> >> TStringList has a way of splitting a single string into several items. >> Assign the CommaText property, and the string list will be filled with >> the different fields of the single input. Its method of splitting the >> string might be OK for your needs. It splits on commas, plus whitespace >> and any non-printing characters. Quotation marks allow fields to > contain >> commas and whitespace. >> >> Newer Delphi versions have DelimitedText, so you can split on something >> other than commas, and even newer versions have the StrictDelimiter >> property, to say whether bare whitespace will split fields. >> >>> Also, I need to be able to search through a csv file to find a >>> particular record where a particular field in a record matches a >>> search value; how would I go about doing this? To explain, say the >>> string has First Name, Last Name, Age. How would you find all string >>> that had the last name Smith? >> You look at each one, and decide which part represents the last name, >> and then decide whether it matches Smith. >> > > Why use a TStringList for this task? Surely processing the csv file as a > descendant of TDataset (e.g. TAdoDataset) would be a _much_ easier, e.g. > > AdoDataset1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data > Source=C:\Documents and Settings\chris\My Documents\;Extended > Properties="text;HDR=Yes;FMTÞlimited";'; > AdoDataset1.CommandText := 'SELECT * FROM [MyFile.csv] WHERE [Last > Name]="Smith"' I never would have considered invoking a Microsoft database library as a "much simpler" way of processing what is little more than a plain text file. It's another dependency, and I've never had much confidence in its reliability, as far as being sure it's installed and configured properly. -- Rob[ Auf dieses Posting antworten ]
