Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 04 Dec 2012 09:03:19 -0600 From: John Deubert Organization: Acumen Training Newsgroups: comp.lang.postscript Date: Tue, 4 Dec 2012 07:03:19 -0800 Message-ID: <2012120407031929493-john@acumentrainingcom> References: <87624kp6hc.fsf@Compaq.site.inet> <2012120313202243338-john@acumentrainingcom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: Putting lines from a file into an array User-Agent: Unison/2.1.10 Lines: 51 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-qZeoE9Ifb+2KrwDAbDV8VU5nerS9cfvaq1HCogbskDqmlvHd4o7HaRzqmypBCktmpaIDhVPhFX0uuI7!lNU9/F0xznWjozn5B37B6pBtO2IVXa2HrVFaAXbW9XtlGs8KfmomHTg3+kgyFmuRDaWKiwI4 X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3248 Xref: csiph.com comp.lang.postscript:1141 On 2012-12-04 09:09:40 +0000, Chris said: > On Mon, 03 Dec 2012 13:20:22 -0800, John Deubert wrote: > > >> As I say, not as simple as Chris' version, but it does work around the >> stack limitation. I'd go with Chris' code unless the number of lines was >> likely to be northward of 1000 lines or so. (By the way, there's some >> cleanup one could do on this code, but lunch is beckoning.) > > FWIW, that wasn't "my version": I tried to answer the question by making > minimal changes to the OP's code. > > If I were writing it from scratch myself, I would either (assuming we > know we'd always have a positionable file stream, or that we're allowed > to use ReusableStreamDecode) not read in the entire file at all, but just > find and keep the line(s) of interest at a given time. > > *Or* I would use (sort of) half of your solution, and use a dictionary as > the "permanent" storage for the file contents, with the line numbers as > the keys. In addition, I would have a single, large line buffer which > gets reused for reading from the file, then I would get the actual length > of each line, and copy it to a new, correctly sized string. Thus > minimising redundant memory use. > > Creating and copying the strings and writing to the dictionary would be > appreciably slower, *but* the bottleneck will always be reading from the > file. And on a decent interpreter, there shouldn't be an appreciable > difference in performance between iterating through a dictionary vs an > array. > > I feel both those would scale better than using an array. > > Chris No offense intended. I assumed you knew about the stackoverflow problem and were presenting a minimum-change solution. I called it "Chris' solution" because, um, well, I had to call it *something.* - J -- ======== John Deubert Acumen Training PostScript & PDF Engineering Classes & Consulting www.acumentraining.com Learn PostScript programming techniques Read the free Acumen Journal acumentraining.com/acumenjournal.html