Subscribe Bookmark RSS Feed

How do I read in a text file line by line.

thickey1

Community Trekker

Joined:

Jun 4, 2015

It is possible to read in a JSL Script line by line (actually JMP makes a list with each line as a list item) using the code below.

 

myFile= open("$PATH\testScript.jsl");
myCfLines= myFile[scriptBox(1)] << getLines();

 

However, when I try to do this with a text file which has a '*.txt' extension JMP automatically opens this as a data table and so I cannot use this method to get the lines (as it's  not a script box!)

 

Is there another method? I dont want to use the loadTextFile() Function as this scrapes the whold file into a string. Ideally I'd like to open the file, read in each line one at a time and do a regexp to pull out the data I need!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

Solution

JSL doesn't currently have tools to open a file and read it one line at a time.

 

However, if you do use Load Text File() then you can use the Words() function to get in the individual lines into a list.

 

bctxt=load text file ("$SAMPLE_IMPORT_DATA\Bigclass.txt");

lines=words(bctxt,"\!n");
-Jeff
4 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

As you see, JMP uses the file extensions to try to do an appropriate thing.

Not sure I completely understand your use case, but if a '.jsl' extension gives the behaviour you want, you could consider using 'RenameFile()':

 

renameFile("$DESKTOP/Test.txt", "Test.jsl");
myFile = open("$DESKTOP/Test.jsl");
myCfLines = myFile[scriptBox(1)] << getLines();
renameFile("$DESKTOP/Test.jsl", "Test.txt");

 

Craige_Hales

Staff

Joined:

Mar 21, 2013

There is a "script" option you can give to open to make this work:

open("$sample_data/../Import Data/bigclass.txt","script")

Now, instead of opening as "text" (which is what the .txt extension is defauilting to) and making a data table, it will open into a script window. Yes, the "text" option was named oddly, but it refers to CSV text. 

Craige
Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

Solution

JSL doesn't currently have tools to open a file and read it one line at a time.

 

However, if you do use Load Text File() then you can use the Words() function to get in the individual lines into a list.

 

bctxt=load text file ("$SAMPLE_IMPORT_DATA\Bigclass.txt");

lines=words(bctxt,"\!n");
-Jeff
thickey1

Community Trekker

Joined:

Jun 4, 2015

Have to give the rewared to Jeff on this one despite my initial reticence on the loadTextFile Function. All three methods work so I did a quick time comparison using a 250M, 5M Line text file from one of our testers. Incidentally my use case is that I need to sift though these huge log files, line by line, and filter out some results based on a table of Regular Expressions.

 

I suppose this isn't fair as I did say I wanted to avoud loadTextFile ;-)

 

***Ian ***

myNumSeconds = 66;
N Items(myCfLines) = 5059756;

***Jeff ***

myNumSeconds = 7;
N Items(myLines) = 5059755;

*** Craige ***

myNumSeconds = 67;
N Items(myCfLines) = 5059756;