Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #1134

Re: Putting lines from a file into an array

From "luser.droog" <luser.droog@gmail.com>
Newsgroups comp.lang.postscript
Subject Re: Putting lines from a file into an array
Date 2012-12-02 23:28 -0600
Organization unorganized
Message-ID <k9hdaa$7pf$1@dont-email.me> (permalink)
References <87624kp6hc.fsf@Compaq.site.inet> <k9gput$d8j$1@dont-email.me> <87r4n8nd8x.fsf@Compaq.site.inet>

Show all headers | View raw


Cecil Westerhof wrote:

> Op zondag 2 dec 2012 22:31 CET schreef luser droog:
> 
>> Chris's response is totally correct. But here's another way to do it.
>> You can slurp the whole file into one string (assuming the file size is
>> less than 65535 bytes), and then use `search` to chop it up.
>>
>> %!
>> %farr.ps File into Array
>> /infile (farr.ps)(r)file def
>> infile dup bytesavailable string readstring pop
>> (\n){ search { 3 1 roll }{ exit } ifelse }loop
>> count array astore ==
>>
>> gs output:
>>
>> [(%!) (%farr.ps File into Array) (/infile \(farr.ps\)\(r\)file def)
>> [(infile
>> dup bytesavailable string readstring pop) (\(\\n\){ search { 3 1 roll }{
>> exit } ifelse }loop) (count array astore ==) ()]
> 
> I already implemented Chris's way, but I'll look into this one also.
> 

No worries. Some caveats are you usually can't rely on `bytesavailable` for
terminal input or pipes, so you can only slurp a file where the size can be
discovered by the interpreter. And my use of `search` above left an empty
string. Adding `pop` just before `exit` will discard it.
And `count array astore` is just the innards of the `]` operator. Just
trying to show you some of the flexibility. And, as a rule, I try to avoid
multiline `{}{}ifelse` constructs because they nest so clumsily. A tight
little block like this I find much easier to read. And using `count` is
also an `assert(stack-is-empty-at-start-of-paragraph)`, cause clearly it
would be wrong if it weren't! :)

Back to comp.lang.postscript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Putting lines from a file into an array Cecil Westerhof <Cecil@decebal.nl> - 2012-12-02 21:02 +0100
  Re: Putting lines from a file into an array Chris <cjl@spamcop.net> - 2012-12-02 20:47 +0000
    Re: Putting lines from a file into an array Cecil Westerhof <Cecil@decebal.nl> - 2012-12-03 00:52 +0100
      Re: Putting lines from a file into an array Don Lancaster <don@tinaja.com> - 2012-12-05 11:58 -0700
        Re: Putting lines from a file into an array "luser.droog" <luser.droog@gmail.com> - 2012-12-05 23:19 -0600
    Re: Putting lines from a file into an array John Deubert <john@acumentraining.com> - 2012-12-03 13:20 -0800
      Re: Putting lines from a file into an array "luser.droog" <luser.droog@gmail.com> - 2012-12-04 00:58 -0600
      Re: Putting lines from a file into an array Chris <cjl@spamcop.net> - 2012-12-04 09:09 +0000
        Re: Putting lines from a file into an array John Deubert <john@acumentraining.com> - 2012-12-04 07:03 -0800
          Re: Putting lines from a file into an array Chris <cjl@spamcop.net> - 2012-12-04 17:46 +0000
        Re: Putting lines from a file into an array Cecil Westerhof <Cecil@decebal.nl> - 2012-12-04 17:17 +0100
  Re: Putting lines from a file into an array "luser.droog" <luser.droog@gmail.com> - 2012-12-02 15:31 -0600
    Re: Putting lines from a file into an array Cecil Westerhof <Cecil@decebal.nl> - 2012-12-03 02:19 +0100
      Re: Putting lines from a file into an array "luser.droog" <luser.droog@gmail.com> - 2012-12-02 23:28 -0600
        Re: Putting lines from a file into an array Cecil Westerhof <Cecil@decebal.nl> - 2012-12-03 10:01 +0100
  Re: Putting lines from a file into an array luser droog <luser.droog@gmail.com> - 2016-08-01 13:25 -0700
    Re: Putting lines from a file into an array JohnF <john@please.see.sig.for.email.com> - 2016-08-02 03:39 +0000
      Re: Putting lines from a file into an array luser droog <luser.droog@gmail.com> - 2016-08-02 21:58 -0700
        Re: Putting lines from a file into an array Tonton Th <tTh@nowhere.invalid> - 2016-08-03 05:10 +0000
        Re: Putting lines from a file into an array luser droog <luser.droog@gmail.com> - 2016-08-02 22:20 -0700
          Re: Putting lines from a file into an array luser droog <luser.droog@gmail.com> - 2016-08-02 22:35 -0700
            Re: Putting lines from a file into an array JohnF <john@please.see.sig.for.email.com> - 2016-08-03 06:21 +0000
            Re: Putting lines from a file into an array JohnF <john@please.see.sig.for.email.com> - 2016-08-03 06:32 +0000
              Re: Putting lines from a file into an array luser droog <luser.droog@gmail.com> - 2016-08-22 22:31 -0700

csiph-web