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
tsandidge
Level III

substitute() for each row() for each column erroring out of bounds

I need to search and replace strings across an entire data table. I referenced this solution, hoping for something elegant, but it's complaining about row 11 in my 10-row table. 

 

Code running on JMP Pro 16.1

allCols = file << get column names(string);
file << group columns("col group", myCols);
myCols = file << get column group("col group");
for(cols = 1, cols <= n items(myCols), cols++,
	file << begin data update;
	for each row(file,
		file:myCols[cols] = substitute(file:myCols[cols],
			"X", "0"
		)
	);
	file << end data update;
);

Error:

Cannot set value for the column 'dot' because the row number (11) is not valid. 
at row 1 in access or evaluation of 'Assign' , file:myCols[cols] = /*###*/Substitute( file:myCols[cols], "X", "0" ) /*###*/

Forgot to add a sample column:

64.0
47.0
2.0
106.0
174.0
12.0
X
2.0
X
14.0

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: substitute() for each row() for each column erroring out of bounds

The below slight modification of your code appears to work OK

Names Default To Here( 1 );
file = Current Data Table();
allCols = file << get column names( string );
file << group columns( "col group", allCols );
myCols = file << get column group( "col group" );
For( cols = 1, cols <= N Items( myCols ), cols++,
	file << begin data update;
	For Each Row(
		file,
		As Column( file, myCols[cols] ) = Substitute( As Column( file, myCols[cols] ),
			"X", "0"
		)
	);
	file << end data update;
);
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: substitute() for each row() for each column erroring out of bounds

The below slight modification of your code appears to work OK

Names Default To Here( 1 );
file = Current Data Table();
allCols = file << get column names( string );
file << group columns( "col group", allCols );
myCols = file << get column group( "col group" );
For( cols = 1, cols <= N Items( myCols ), cols++,
	file << begin data update;
	For Each Row(
		file,
		As Column( file, myCols[cols] ) = Substitute( As Column( file, myCols[cols] ),
			"X", "0"
		)
	);
	file << end data update;
);
Jim
tsandidge
Level III

Re: substitute() for each row() for each column erroring out of bounds

@txnelson , the As Column() did the trick, thank you!