Choose Language Hide Translation Bar
Highlighted
MattP
Level I

Break Nested For Loop Help

Hi,

I am trying to execute a series of nested for loops one at a time so that the inner for loops happen once each time the outer for loop is run through. 

To give a bit more context I have a PDF file which is 58 pages long. Every two pages is an individual document that I want to seperate and open a new table for using the text import wizard (so 29 documents sandwhiched together in one PDF). I also want to have the column names start at the 32nd line and the data start at the 33rd line of each two page document.

To do this I have nested for loops at Column Names Start and Data Start in the text wizard script. There is 134 lines between the column names on each of the 29 documents, and subsequently 134 lines between were the data starts on each of the 29, 2 page long, documents.

There is also a for loop around the entire script to open the main file 29 times, with the subsequent for loops choosing which line to start and and make a table for each document sandwhiched in the file.

How would I use the Break or Continue command to have the script execute the first for loop once, get to the next for loop and have it execute once, then continue down the script executing each for loop only once before starting again at the top of the file and going through the next iteration of the for loop? While also having each for loop remember the past iteration to keep the line count where it should be.

 

Thank you,

Matt

 

//opens text file using text wizard
for(i=1, i<=29, i++,
	Open(
	"C:\Users\file.txt",
	columns(
		New Column( "Parameters", Character, "Nominal" ),
		New Column( "Results", Character, "Nominal" ),
		New Column( "Units", Character, "Nominal" ),
		New Column( "Uncertainty", Character, "Nominal" ),
		New Column( "Spec", Character, "Nominal" ),
		New Column( "Methods", Character, "Nominal" ),
		New Column( "c000007", Character, "Nominal" ),
		New Column( "c000008", Character, "Nominal" ),
		New Column( "c000009", Character, "Nominal" ),
		New Column( "c0000010", Character, "Nominal" ),
		New Column( "c0000011", Character, "Nominal" ),
		New Column( "c0000012", Character, "Nominal" ),
		New Column( "c0000013", Character, "Nominal" ),
		New Column( "c0000014", Character, "Nominal" ),
		New Column( "c0000015", Character, "Nominal" ),
		New Column( "c0000016", Character, "Nominal" ),
		New Column( "c0000017", Character, "Nominal" ),
		New Column( "c0000018", Numeric, "Continuous", Format( "Best", 10 ) )
	),
	Import Settings(
		End Of Line( CRLF, CR, LF ),
		End Of Field( Space, CSV( 0 ) ),
		Strip Quotes( 1 ),
		Use Apostrophe as Quotation Mark( 0 ),
		Use Regional Settings( 0 ),
		Scan Whole File( 1 ),
		Treat empty columns as numeric( 0 ),
		CompressNumericColumns( 0 ),
		CompressCharacterColumns( 0 ),
		CompressAllowListCheck( 0 ),
		Labels( 1 ),
		Column Names Start( 
		
			for(q=1, q<=29, q++,
			(32*q)+for(p=135, p<29, p++,
						(show(p));
					);
			);
		),
		Data Starts( 
		
			for(t=1, t<=29, t++,
			(33*t)+for(s=135, s<29, s++,
						(show(s));
					);
			);
		),
		Lines To Read( "135" ),
		Year Rule( "20xx" )
	)
));
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Craige_Hales
Staff (Retired)

Re: Break Nested For Loop Help

Try something like this

filename = Save Text File( "$temp\x.csv", "a,b,c
	1,2,3
	a,b,c
	2,3,4
	a,b,c
	3,4,5
	" );
dtlist = {};
For( i = 1, i <= 3, i += 1,
    names = (i - 1) * 2 + 1;
    data = names + 1;
    dtlist[i] = Open(
        filename,
        columnNamesStart( names ),
        DataStarts( data ),
        Lines To Read( 1 )
    );
    dtlist[i] << setname( "segment" || Char( i ) );
);

Three tables with names set from JSLThree tables with names set from JSL

Craige

View solution in original post

2 REPLIES 2
Highlighted
uday_guntupalli
Level VIII

Re: Break Nested For Loop Help

@MattP,
           I tried to look through your code and here is a couple of things that I noticed : 

 

image.png

The highlighted sections don't really make much sense to me. I would recommend a different approach if you are willing to try it. Run your for loop and let it read all the data into the data table. While it may be counter-intuitive to read more data than necessary, I think both from efficiency and clarity of your code, it would be much more easier to post-process the data that is read in rather than try to limit what is coming in. So, Keep your outer loop and remove the inner loops in "Column" and "Data" sections. Once you have the data, let us take an example data table and see how you can filter out what you want. Give it a shot and chime in for any help.  

Best
Uday
Highlighted
Craige_Hales
Staff (Retired)

Re: Break Nested For Loop Help

Try something like this

filename = Save Text File( "$temp\x.csv", "a,b,c
	1,2,3
	a,b,c
	2,3,4
	a,b,c
	3,4,5
	" );
dtlist = {};
For( i = 1, i <= 3, i += 1,
    names = (i - 1) * 2 + 1;
    data = names + 1;
    dtlist[i] = Open(
        filename,
        columnNamesStart( names ),
        DataStarts( data ),
        Lines To Read( 1 )
    );
    dtlist[i] << setname( "segment" || Char( i ) );
);

Three tables with names set from JSLThree tables with names set from JSL

Craige

View solution in original post

Article Labels

    There are no labels assigned to this post.