Welcome to %s forums

BrainModular Users Forum

Login Register

Save Array to text file ; read array from text file.

I need help on a Patch
Post Reply
moody33
Member
Posts: 338
Contact:

Unread post by moody33 » 02 Mar 2011, 22:07

Hi again !

I need a script that could save an array to a text file, and another one that could perform the same process in reverse. So , the second one should open a txt file containing an array and extract those array to an array output. Both scripts have to read/write a large amount of array ( near 40 000 elements values) . The second one have to extract the array in one bloc size, ( should not extracts elements by an index).

Since senso have build a cool OpenDialog module, this script should be useful for everyone, and especially for me ! :D
So, if there is any candidate ( follow my eyes ;) ), I 'll be very happy !

Bisette.

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 02 Mar 2011, 23:25

I made a couple of scripts years ago that partly do what you want, you find them in the add-ons/data tools: data2textfile and textfile2data. These are however using the old scripting engine, so I think it's about time I revised them, I guess... :)

Anyone feel free to update them, I wont have time to do anything with them before the weekend.
Bjørn S

User avatar
senso
Site Admin
Posts: 4424
Location: France
Contact:

Unread post by senso » 03 Mar 2011, 00:04

As I can remember there is a script in the /modules/scripts which does the job (inspired from the bsork script)

moody33
Member
Posts: 338
Contact:

Unread post by moody33 » 03 Mar 2011, 01:15

@senso
i've already take a look at the script, but it doesn't seems to correspond to my need since there is no inputs and no ouputs, I don't really understand how to use it.

@Bsork
If you have the time, I'll be happy if you make another one of course. I've already take a look to your former text to data and it seems to content errors ( show in the console ). And it only ouput one value at a time. So , I will wait for another one if possible. :)

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 03 Mar 2011, 08:35

I'll see what I can do during the weekend. I'm not at all surprised that the old script gives errors, as a lot has happened to Usine and the scripting engine since it was made.
Bjørn S

moody33
Member
Posts: 338
Contact:

Unread post by moody33 » 09 Mar 2011, 23:16

Up...( and down ! )

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 10 Mar 2011, 07:52

Sorry to be late, but I've sidetracked myself with testing various script-stuff to improve file error handling and string manipulations. I will upload usable scripts without the extra touches tonight.

I haven't actually decided exactly what the extra touches would be either, so new add-ons will have to wait.
Bjørn S

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 10 Mar 2011, 22:50

Here is a combined script for reading and writing arrays in text files. No fancy stuff, the only check being a FileExists before reading. Hopefully useful.

Code: Select all

VAR pArrayIn    : TParameter;
VAR pArrayOut   : TParameter;
VAR pFileName   : TParameter;
VAR pWrite      : TParameter;
VAR pRead       : TParameter;

PROCEDURE Init;
BEGIN
   pArrayIn := CreateParam('array in', ptArray); SetIsOutput(pArrayIn, FALSE);
   SetMin(pArrayIn, -1000000); SetMax(pArrayIn, 1000000);

   pArrayOut := CreateParam('array out', ptArray); SetIsInput(pArrayOut, FALSE);
   SetMin(pArrayOut, -1000000); SetMax(pArrayOut, 1000000);

   pFileName := CreateParam('file name', ptTextField); SetIsOutput(pFilename, FALSE);

   pWrite := CreateParam('write', ptButton); SetIsOutput(pWrite, FALSE);
   pRead := CreateParam('read', ptButton); SetIsOutput(pRead, FALSE);
END;


PROCEDURE Callback(n : Integer);
   VAR i : Integer;
   VAR list : TStringList;
BEGIN
   CASE n OF
      pWrite : BEGIN
                  list := TStringList.Create;
                  FOR i := 0 TO (GetLength(pArrayIn) - 1) DO
                     list.Add(FloatToStr(GetDataArrayValue(pArrayIn, i)));
                  list.SaveToFile(GetStringValue(pFileName));
                  list.Free;
               END;
     pRead   : IF (FileExists(GetStringValue(pFileName))) THEN BEGIN
                  list := TStringList.Create;
                  list.LoadFromFile(GetStringValue(pFileName));
                  SetLength(pArrayOut, list.Count);
                  FOR i := 0 TO (list.Count - 1) DO
                     SetDataArrayValue(pArrayOut, i, StrToFloat(list[i]));
                  list.Free;
               END
               ELSE
                  WriteLn('Couldn''t open file: ' + GetStringValue(pFileName));
   END;
END; // Callback
Bjørn S

moody33
Member
Posts: 338
Contact:

Unread post by moody33 » 10 Mar 2011, 23:21

Really nice Bsork ! I 'll take a look asap.

Hope you had fun writing it ! :)

Thanks you very much.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests