cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
dadawasozo
Level IV

read zip file, if contains "string" then add row to a table with value label

Hi,

I created a table then open a zip folder, first save a log file to specific path, second read a file (long text file) then check if it contains string. if yes, add new row to created table and add value. everything works until the adding new row and value to table. I cannot figure out what is the issue because there is no error or information in log. Below is my script, can someone give suggestion what is the issue?

dt = open(sometable);
lst2 = column(dt1, "t1") << Get Values;
dt2 = new table("data_"||todaydate,
	new column("t1", character),
	newcolumn("data", character)
);

for (k=1, k <=N items(lst2), k++,

	//print (lst2[k]);
	
	Try(
    za = open(path2||lst2[k], zip);
    dirlist = za << dir;
    //show(dirlist); //check the list
    
    for (l=1, l<=N Items(dirlist), l++,
		If (Left(dirlist[l],5) == "tests" & Right(dirlist[l],8) == "_raw.log" ,   
			show(dirlist[l]);
			text = za << read(dirlist[l]);
			//print (dirlist[l]);
			save text file(path1|| Format Date( Today(), "ddmonyyyy" ) ||"\"||lst2[k]||"_"||dirlist[l],text);
			,
			dirlist[l]== "line.log" ,
			text = za << read(dirlist[l]);
				
				if(contains(text, "string"),
				dt2 << add rows (t1 = lst2[k], data = "Yes"),
				dt2 << add rows (t1 = lst2[k], data = "No")
					
				);
				
			
			);
   
		)
		,
    print (lst2[k] || "fail to open");
    Throw();
		);
 );




1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: read zip file, if contains "string" then add row to a table with value label

I think you have the answer, just to clarify:

dt = Open( sometable );
lst2 = Column( dt1, "t1" ) << Get Values;
dt2 = New Table( "data_" || todaydate, New Column( "t1", character ), New Column( "data", character ) );

For( k = 1, k <= N Items( lst2 ), k++, 
	Try(
		za = Open( path2 || lst2[k], zip );
		dirlist = za << dir;
		For( l = 1, l <= N Items( dirlist ), l++,
			If(	Left( dirlist[l], 5 ) == "tests" & Right( dirlist[l], 8 ) == "_raw.log",
				Show( dirlist[l] );
				text = za << read( dirlist[l] );
				Save Text File( path1 || Format Date( Today(), "ddmonyyyy" ) || "\" || lst2[k] || "_" || dirlist[l], text );
			,/*else if*/ dirlist[l] == "line.log",
				Try(
					text = za << read( dirlist[l] );
					If( Contains( text, "string" ),
						dt2 << add rows( t1 = lst2[k], data = "Yes" ),
						dt2 << add rows( t1 = lst2[k], data = "No" )
					);
				,/* catch*/
					Print( lst2[k] || "cannot read" );
					Throw(); // this throw will end the for-L loop
				);
			);
		);
	,/*catch*/
		Print( lst2[k] || "fail to open" );
		Throw(); // this throw will end the for-K loop
	)
);
Craige

View solution in original post

8 REPLIES 8
dadawasozo
Level IV

Re: read zip file, if contains "string" then add row to a table with value label

I think I found the issue. Need the {} . It worked without {} before. Anyone has insight why now need the {}? 

 

dt2 << add rows ({t1 = lst2[k], data = "Yes"}),
dt2 << add rows ({t1 = lst2[k], data = "No"})
					

 

Craige_Hales
Super User

Re: read zip file, if contains "string" then add row to a table with value label

Help->Scripting IndexHelp->Scripting Index

The doc has been like this since at least JMP 14. Are you seeing a change in behavior between versions?

Craige
dadawasozo
Level IV

Re: read zip file, if contains "string" then add row to a table with value label

Hi Craige,

 

I was doing below and success, when only add rows and value to one of the column.

dt2 = new table("data_"||todaydate,
	new column("t1", character),
	newcolumn("data", character)
);

for (k=1, k <=N items(lst2), k++,

dt2 << add rows (t1 = lst2[k])
);

 

Craige_Hales
Super User

Re: read zip file, if contains "string" then add row to a table with value label

If lst2[k] was a number, 1 perhaps, I can see how it might have seemed to be working. The assignment would be evaluated, changing t1 of the (old) currentrow, then the result (in t1) would be the number of rows to add. But those row(s) would not be assigned a value until the next time through the loop. But that doesn't quite fit with what you have (a character t1), so maybe not.

Glad you've got it figured out; the list does not evaluate the items in the list. Instead, the list is passed into the add rows method, and addrows uses the list on the freshly added row.

Craige
dadawasozo
Level IV

Re: read zip file, if contains "string" then add row to a table with value label

I have one following question about try() and throw() in my original script above. I encounter a situation that the try() + throw() not skip and continue to next in loop. The issue happens at reading the line.log. One of the line.log is so huge that it cannot be opened. I concluded as it because I tried manually to open the specific line.log and it gave me an error saying the file is too big to open by txt. So added another try() and throw() for it but still, it only printed the message of lst2[k]"cannot read" and lst2[k[ "fail to open". then the loop stop at that lst2[k], not going to the next in loop. Did I make a mistake on how to use try() and throw()?

dt = open(sometable);
lst2 = column(dt1, "t1") << Get Values;
dt2 = new table("data_"||todaydate,
	new column("t1", character),
	newcolumn("data", character)
);

for (k=1, k <=N items(lst2), k++,

	//print (lst2[k]);
	
	Try(
    za = open(path2||lst2[k], zip);
    dirlist = za << dir;
    //show(dirlist); //check the list
    
    for (l=1, l<=N Items(dirlist), l++,
		If (Left(dirlist[l],5) == "tests" & Right(dirlist[l],8) == "_raw.log" ,   
			show(dirlist[l]);
			text = za << read(dirlist[l]);
			//print (dirlist[l]);
			save text file(path1|| Format Date( Today(), "ddmonyyyy" ) ||"\"||lst2[k]||"_"||dirlist[l],text);
			,
			dirlist[l]== "line.log" ,
try( text = za << read(dirlist[l]); if(contains(text, "string"), dt2 << add rows (t1 = lst2[k], data = "Yes"), dt2 << add rows (t1 = lst2[k], data = "No") );
,
print(lst2[k] || "cannot read");
throw();
); ); ) , print (lst2[k] || "fail to open"); Throw(); ); );
Craige_Hales
Super User

Re: read zip file, if contains "string" then add row to a table with value label

I reformatted the code (control-M) to see the structure and I think you want to leave out the throw()

The error from <<read was caught, then re-thrown to the outer try,The error from <<read was caught, then re-thrown to the outer try,

You probably don't want the outer throw either. Sometimes using the throw() like that is desirable, if you really mean you have not finished handling the exception and want it to propagate to a containing handler, but at least for the inner try I think you have taken care of it and want the loop to continue.

Craige
dadawasozo
Level IV

Re: read zip file, if contains "string" then add row to a table with value label

remove only inner throw() the script output all lst2[k] "cannot read". However, if remove both inner and outer throw()s, the script run as designed.

Craige_Hales
Super User

Re: read zip file, if contains "string" then add row to a table with value label

I think you have the answer, just to clarify:

dt = Open( sometable );
lst2 = Column( dt1, "t1" ) << Get Values;
dt2 = New Table( "data_" || todaydate, New Column( "t1", character ), New Column( "data", character ) );

For( k = 1, k <= N Items( lst2 ), k++, 
	Try(
		za = Open( path2 || lst2[k], zip );
		dirlist = za << dir;
		For( l = 1, l <= N Items( dirlist ), l++,
			If(	Left( dirlist[l], 5 ) == "tests" & Right( dirlist[l], 8 ) == "_raw.log",
				Show( dirlist[l] );
				text = za << read( dirlist[l] );
				Save Text File( path1 || Format Date( Today(), "ddmonyyyy" ) || "\" || lst2[k] || "_" || dirlist[l], text );
			,/*else if*/ dirlist[l] == "line.log",
				Try(
					text = za << read( dirlist[l] );
					If( Contains( text, "string" ),
						dt2 << add rows( t1 = lst2[k], data = "Yes" ),
						dt2 << add rows( t1 = lst2[k], data = "No" )
					);
				,/* catch*/
					Print( lst2[k] || "cannot read" );
					Throw(); // this throw will end the for-L loop
				);
			);
		);
	,/*catch*/
		Print( lst2[k] || "fail to open" );
		Throw(); // this throw will end the for-K loop
	)
);
Craige