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


Groups > comp.soft-sys.math.mathematica > #13482 > unrolled thread

Reading a file in reverse order --- How?

Started byVirgil Stokes <vs@it.uu.se>
First post2013-02-14 07:08 +0000
Last post2013-04-11 08:11 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Reading a file in reverse order --- How? Virgil Stokes <vs@it.uu.se> - 2013-02-14 07:08 +0000
    Re: Reading a file in reverse order --- How? debguy <johnandsara2@cox.net> - 2013-04-10 04:48 +0000
    Re: Reading a file in reverse order --- How? Waclaw Kusnierczyk <waku@idi.ntnu.no> - 2013-04-11 08:11 +0000
    Re: Reading a file in reverse order --- How? plank.in.sequim@gmail.com - 2013-04-11 08:11 +0000

#13482 — Reading a file in reverse order --- How?

FromVirgil Stokes <vs@it.uu.se>
Date2013-02-14 07:08 +0000
SubjectReading a file in reverse order --- How?
Message-ID<kfi2i3$d0p$1@smc.vnet.net>
Here is a very simple example of saving lists (writing to a file) and then 
reading the lists back in the same order as they were written.  The problem that 
I would like to get a solution to is how can the lists be *read in reverse 
order?* Thus, the first list saved would be the last list to be inputted.

Note, in general, the length of each list is the same and rather small; but, the 
number of lists written to the file can be rather large (n > 10,000), at least 
too large to keep all of them in memory.

fname = "testfile";

strm = OpenWrite[fname];
n = 3; (* In general, n could be very large *)
For[k = 1, k <= n, k++,
  (* Create list on each pass through this loop ... *)

  POt = {{k, k + 1}, {k + 2, k + 3}};
  (* Print[POt]; *)
  (* Save list to a file *)
  Write[strm, POt];
  ];
Close[strm];

(* Other processing performed... *)

(* Open file with lists *)
strm = OpenRead[fname];
(* Process, sequentially, each list that was saved *)
For[i = 1, i <= n, i++,
   PIn = Read[strm];
   (* Print[PIn]; *)

   (* Process PIn ... *)

   ];
Close[strm];

[toc] | [next] | [standalone]


#14121

Fromdebguy <johnandsara2@cox.net>
Date2013-04-10 04:48 +0000
Message-ID<kk2qvb$bie$1@smc.vnet.net>
In reply to#13482
# file in reverse order use tac(1)
$ cat file | tac

[toc] | [prev] | [next] | [standalone]


#14144

FromWaclaw Kusnierczyk <waku@idi.ntnu.no>
Date2013-04-11 08:11 +0000
Message-ID<kk5r7m$rj2$1@smc.vnet.net>
In reply to#13482
On 04/10/2013 06:52 AM, debguy wrote:
> # file in reverse order use tac(1)
> $ cat file | tac

# file in reverse order use just tac(1)
tac file

[toc] | [prev] | [next] | [standalone]


#14147

Fromplank.in.sequim@gmail.com
Date2013-04-11 08:11 +0000
Message-ID<kk5r70$ril$1@smc.vnet.net>
In reply to#13482
One possibility is to keep track of the stream positions for each entry when writing out and then SetStreamPosition to those positions in reverse order when reading each record back in.  You ought to have enough memory for a 10K integer array.  Argh!  I can't seem to get Mathematica to put out a nice text for pasting.  Will MathML work?  Anyway, hopefully you can make some sense of this.  It's not that big a change from your original code.

fname="testfile";
entryLocs={};
Print["Input"];
strm=OpenWrite[fname];
n=3;(*In general,n could be very large*)For[k=1,k<=n,k++,(*Create list on each pass through this loop...*)POt={{k,k+1},{k+2,k+3}};
entryLocs=Append[entryLocs,StreamPosition[strm]];
Print[POt];(*Save list to a file*)Write[strm,POt];];
Close[strm];
(*Other processing performed...*)
(*Open file with lists*)
strm=OpenRead[fname];
(*Process,sequentially,each list that was saved*)
For[i=1,i<=n,i++,
SetStreamPosition[strm,entryLocs[[n-i+1]]];
PIn=Read[strm];

Print[PIn];(*Process PIn...*)];

Close[strm];

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web