POV-Ray : Newsgroups : povray.advanced-users : Reading directly into an Array Server Time
26 Nov 2024 13:39:45 EST (-0500)
  Reading directly into an Array (Message 1 to 2 of 2)  
From: David Vincent-Jones
Subject: Reading directly into an Array
Date: 19 Mar 2000 17:05:02
Message: <38d54f0e@news.povray.org>
I have an ASCII file that I wish to read into an array.
File is 117 records separated by CR/LF and
each record has 6 text elements separated by a comma.

#fopen SymFile "c:\pf\bin\sym.asc" read
#declare Syms=array [117] [6] { #fread (SymFile, defined)}

Can somebody steer me in the right direction..

David


Post a reply to this message

From: Chris Colefax
Subject: Re: Reading directly into an Array
Date: 19 Mar 2000 18:21:48
Message: <38d5610c@news.povray.org>
David Vincent-Jones <geo### [at] galaxynetcom> wrote:
> I have an ASCII file that I wish to read into an array.
> File is 117 records separated by CR/LF and
> each record has 6 text elements separated by a comma.
>
> #fopen SymFile "c:\pf\bin\sym.asc" read
> #declare Syms=array [117] [6] { #fread (SymFile, defined)}
>
> Can somebody steer me in the right direction..

To use the data with POV-Ray's file I/O the text values will need to be
quoted, and there will need to be commas at the end of every line (i.e. a
newline does not count as a delimiter).  Then you could read in the file so:

   #fopen F, "MyFile.txt", read
   #local MyArray = array[117][6]

   #local M = 0; #while (M < 117)
      #local N = 0; #while (M < 6)
         #read (F, Data)
         #declare MyArray[M][N] = Data
      #local N = N + 1; #end
   #local M = M + 1; #end

   #fclose F

The temporary String variable is required as the #read command cannot save
the file data directly to an array variable.  An alternative is to modify
the data file so it begins with the line:

   array[117][6] {

Then add opening and closing brackets to each line:

   { "data1", "data2", "data3", "data4", "data5", "data6" },

and end with a final closing bracket (and no comma after the last subgroup):

   }

This way you can include the file directly into your scene (e.g. #declare
MyArray = #include "MyArray.inc") which may be quicker to parse than using
the #read command.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.