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
SpannerHead
Level IV

Eliminate empty rows from a script

I have a script that builds a second script inside a JMP table, copies that and pastes it to a JMP script window to make a functional script.  Because of the nature of how this is achieved, I get a lot of empty rows in the finished script and I'd like to eliminate those.  Reformat doesn't do the trick and my attempts at REGEX and GLOBALREPLACE also fail.  Anyone succeed with this?

 

I looked in here for clues but I'm still not there.

 

Scripting the Script Editor (jmp.com)


Slán



SpannerHead
2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Eliminate empty rows from a script

Line changing whitespace characters are not all the same which can cause some problems. Sometimes Words() can be clean option for something like this

Names Default To Here(1);

nw = New Window("This is a script box",
	H List Box(
		Panel Box("Long Script",
			sb1 = Script Box(
				"print(\!" Hello\!");

			print(\!" Hello\!");

			print(\!" Hello\!");











			// comment
			print(\!" Hello\!");



			// comment
			print(\!" Hello\!");",
				600,
				500
			);

		),
		sb2 = Script Box(, 600, 500)
	)
);

// You can also clear tabs if you wish to \t (regex) or \!t (jmp)
Wait(1);
sb2 << Set Text(Regex(sb1 << get text, "[\r\n]+", "\!N", GLOBALREPLACE));
sb2 << reformat;

Wait(1);
sb2 << Set Text("");

Wait(1);
sb2 << Set Text(Concat Items(Words(sb1 << get text, "\!N"), "\!N"));
sb2 << reformat;


Wait(1);
sb2 << Set Text("");

Wait(1);
lines = sb1 << get lines;
empty = Loc(ls, "");
sb2 << Set Text(Concat Items(Remove(lines, As List(empty)), "\!N"));
sb2 << reformat;

In JMP

jthi_0-1726034628624.png

JSL Syntax Rules (jmp.com)

 

In regex (usually)

jthi_1-1726034690030.png

-Jarmo

View solution in original post

SpannerHead
Level IV

Re: Eliminate empty rows from a script

Jarmo

 

Even better, I don't need to have parallel script windows.  I can do the elimination in a single window with this.  I nominate the present script window as ed and I'm off to the the races.  Thanks for the help.

 

	ed = ww[Script Box(1)];
	Wait(1);
	ed << Set Text(Regex(ed << get text, "[\r\t]+", "\!N", GLOBALREPLACE));
	ed << reformat;

 


Slán



SpannerHead

View solution in original post

8 REPLIES 8
txnelson
Super User

Re: Eliminate empty rows from a script

Here is one idea.  It takes a script and basically removes all line feeds and then reformats the script.

The reformatted script realigns the code

Names Default To Here( 1 );
nw = New Window( "This is a script box",
sb = Script Box( "print(\!" Hello\!");

print(\!" Hello\!");

print(\!" Hello\!");
print(\!" Hello\!");



print(\!" Hello\!");", 300, 100 );

);
sb << Save Text( "$TEMP/Test.jsl" );
nw << close window;
intText = Load Text File( "$TEMP/Test.jsl" );
substitute into(intText,hextochar("0D"), " ");

nw = New Window( "This is a new script box",
sb = Script Box()
);

sb << Append Text( intText );
sb << reformat;

Jim
hogi
Level XII

Re: Eliminate empty rows from a script

If you can live with the fact that all comments get removed as well [like in Formula Editor], you can use the magic of Char or Parse:

 

myScript = Char(
	Expr(
		For( i = 1, i <= 5, i++, 
        // Print the value of i.
			Print( i );
        
        
        
        
        
		);
    // End expression.
	)
);


nw = New Window( "test", <<Type( "Script" ), myScript );
wait(0);
Main Menu( "reformat script " );

 

 

 

myScript = Parse( "/* Begin quote. */
    For (i = 1, i <= 5, i++,
        // Print the value of i.
        Print(i);
        
        
        
        
        
    );
    // End expression.");
   write(myscript)

 

by the way: via <<Type( "Script" )
the new window is a standard Script Editor window - with the same look and feel (e.g. a play button to run the script : )

txnelson
Super User

Re: Eliminate empty rows from a script

I refined my code a bit, which is not a perfect solution, but it may work

Given

txnelson_0-1725993895561.png

It returns

txnelson_1-1725993951732.png

Names Default To Here( 1 );
nw = New Window( "This is a script box",
sb = Script Box( "print(\!" Hello\!");

print(\!" Hello\!");

print(\!" Hello\!");











// comment
print(\!" Hello\!");



// comment
print(\!" Hello\!");", 300, 100 );

);
sb << Save Text( "$TEMP/Test.jsl" );
nw << close window;
intText = Load Text File( "$TEMP/Test.jsl" );
substitute into(intText,hextochar("0D0D"), "\!n");
substitute into(intText,hextochar("0A0A"), " ");
substitute into(intText,hextochar("2F2F"), "\!n//");

nw = New Window( "This is a new script box",
sb = Script Box()
);

sb << Append Text( intText );
sb << reformat;
Jim
SpannerHead
Level IV

Re: Eliminate empty rows from a script

Thanks Jim and Hogi.  I'm surprised how complicated this actually is.  I had thought something that kills off incidences of double carriage returns would have worked in this case.  Maybe Regex statements work on JMP tables but not scripts?

 

ww = New Window( (names), << Script );
ed = ww[Script Box(1)];
Regex( ed, "\n\n", "", GLOBALREPLACE )

 


Slán



SpannerHead
hogi
Level XII

Re: Eliminate empty rows from a script

yes, sure, in JMP there are always 12 ways to do what you want to do ...

ww = New Window( "title", << Script );
ed = ww[Script Box(1)];

new window("helper", Button Box ("clean",
mytext = ed << get text;
ed << set text(Regex( mytext, "[\n\r]{2,}", "\n", GLOBALREPLACE ))))
jthi
Super User

Re: Eliminate empty rows from a script

Line changing whitespace characters are not all the same which can cause some problems. Sometimes Words() can be clean option for something like this

Names Default To Here(1);

nw = New Window("This is a script box",
	H List Box(
		Panel Box("Long Script",
			sb1 = Script Box(
				"print(\!" Hello\!");

			print(\!" Hello\!");

			print(\!" Hello\!");











			// comment
			print(\!" Hello\!");



			// comment
			print(\!" Hello\!");",
				600,
				500
			);

		),
		sb2 = Script Box(, 600, 500)
	)
);

// You can also clear tabs if you wish to \t (regex) or \!t (jmp)
Wait(1);
sb2 << Set Text(Regex(sb1 << get text, "[\r\n]+", "\!N", GLOBALREPLACE));
sb2 << reformat;

Wait(1);
sb2 << Set Text("");

Wait(1);
sb2 << Set Text(Concat Items(Words(sb1 << get text, "\!N"), "\!N"));
sb2 << reformat;


Wait(1);
sb2 << Set Text("");

Wait(1);
lines = sb1 << get lines;
empty = Loc(ls, "");
sb2 << Set Text(Concat Items(Remove(lines, As List(empty)), "\!N"));
sb2 << reformat;

In JMP

jthi_0-1726034628624.png

JSL Syntax Rules (jmp.com)

 

In regex (usually)

jthi_1-1726034690030.png

-Jarmo
SpannerHead
Level IV

Re: Eliminate empty rows from a script

Jarmo

 

Mighty!  Had to tinker with it a bit, seems like there was a Tab that I didn't immediately recognise.  This ended up working.

 

Wait(1);
sb2 << Set Text(Regex(sb1 << get text, "[\r\t]+", "\!N", GLOBALREPLACE));
sb2 << reformat;

Slán



SpannerHead
SpannerHead
Level IV

Re: Eliminate empty rows from a script

Jarmo

 

Even better, I don't need to have parallel script windows.  I can do the elimination in a single window with this.  I nominate the present script window as ed and I'm off to the the races.  Thanks for the help.

 

	ed = ww[Script Box(1)];
	Wait(1);
	ed << Set Text(Regex(ed << get text, "[\r\t]+", "\!N", GLOBALREPLACE));
	ed << reformat;

 


Slán



SpannerHead