cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
Jackie_
Level VI

Replace characters in the string

Hi JMP community,

 

I'm trying to write a jsl code that would take a column with a string in it, and in each row, replace the count with the values in the count column. The final results should look something like in the Results column

Any Suggestions?

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: Replace characters in the string

The strings appear to follow the same pattern. Maybe use the words() function with either a space or " as the separator. Then use concatitems() to put it back together.

capture.png

Craige

View solution in original post

jthi
Super User

Re: Replace characters in the string

I think the issue is with this line

x[4] = Char( arra[binxx[j]] ); 

I would modify the loop to loop over the rows instead of associative array + rows:

Names Default To Here(1);

dt = Current Data Table();
/// Get list of all the bins
binxx = Associative Array(:Bins[23 :: N Rows(dt)]) << Get Keys;

delcols = {"", "	", "}"};
/// Remove garbage values																	
For(i = 1, i <= N Items(delcols), i++,
	Remove From(binxx, As List(Loc(binxx, delcols[i])));
);

///Get rows where Bins column contains BIN = 
rows = dt << get rows where(Contains(:Bins, "BIN = "));
total = {};

//Loop to calculate the count of each bins
For(i = 1, i <= N Items(binxx), i++, 
	Insert Into(total, N Items(dt << get rows where(:Bins == binxx[i])))
);

//create array to store bin and counts
arra = Associative Array(binxx, total);

//loop to modify the character count in the table
For(i = 1, i <= N Items(rows), i++, 
	x = Words(:Bins[rows[i]]);
	curbin = Substitute(x[3], "\!"", "");
	If(Contains(arra, curbin),
		x[4] = Char(arra[curbin]);
		:Bins[rows[i]] = Concat Items(x);
	,
		continue();
	);
);
-Jarmo

View solution in original post

6 REPLIES 6
Craige_Hales
Super User

Re: Replace characters in the string

The strings appear to follow the same pattern. Maybe use the words() function with either a space or " as the separator. Then use concatitems() to put it back together.

capture.png

Craige
jthi
Super User

Re: Replace characters in the string

Regex is one other option

Regex(:Results, "(BIN = \!".+\!") (\d+)(\!".+)", "\1 " || Char(:Count) || "\3")

but in this case I would use Words() + Concat Items() like Craige suggested (if the pattern is always same)

-Jarmo
Jackie_
Level VI

Re: Replace characters in the string

Thanks Craig and Jarmo 

Jackie_
Level VI

Re: Replace characters in the string

@jthi 
I used Cragis logic to update the count in the string but it doesn't replaces the correct count string. I'm not sure what I am doing wrong. Can you take a look at my code?

 

Assigns incorrect counts 

 

 



Here's the code:

dt = Current Data Table();
/// Get list of all the bins
binxx = Associative Array( :Bins[23:: N Rows( dt )] ) << Get Keys;

delcols = {"", "	", "}"};
/// Remove garbage values																	
For( i = 1, i <= N Items( delcols ), i++,
	Remove From( binxx, As List( Loc( binxx, delcols[i] ) ) );
	
	
);

///Get rows where Bins column contains BIN = 
rows = dt << get rows where( Contains( :Bins, "BIN = " ) );
total = {};

//Loop to calculate the count of each bins
For( i = 1, i <= N Items( binxx ), i++, 
	
	
	Insert Into( total, N Items( dt << get rows where( :Bins == binxx[i] ) ) )
);
//create array to store bin and counts
arra = Associative Array( binxx, total );

//loop to modify the character count in the table

For( j = 1, j <= N Items( binxx ), j++,
	For( i = 2, i <= N Items( rows ), i++, 
	
		If( Contains( arra, Substr( :Bins[i], 8, 1 ) ), 
		
			x = Words( :Bins[rows[i]] );
			x[4] = Char( arra[binxx[j]] ); 

			:Bins[rows[i]] = Concat Items( x );

		)
	)
);


 

jthi
Super User

Re: Replace characters in the string

I think the issue is with this line

x[4] = Char( arra[binxx[j]] ); 

I would modify the loop to loop over the rows instead of associative array + rows:

Names Default To Here(1);

dt = Current Data Table();
/// Get list of all the bins
binxx = Associative Array(:Bins[23 :: N Rows(dt)]) << Get Keys;

delcols = {"", "	", "}"};
/// Remove garbage values																	
For(i = 1, i <= N Items(delcols), i++,
	Remove From(binxx, As List(Loc(binxx, delcols[i])));
);

///Get rows where Bins column contains BIN = 
rows = dt << get rows where(Contains(:Bins, "BIN = "));
total = {};

//Loop to calculate the count of each bins
For(i = 1, i <= N Items(binxx), i++, 
	Insert Into(total, N Items(dt << get rows where(:Bins == binxx[i])))
);

//create array to store bin and counts
arra = Associative Array(binxx, total);

//loop to modify the character count in the table
For(i = 1, i <= N Items(rows), i++, 
	x = Words(:Bins[rows[i]]);
	curbin = Substitute(x[3], "\!"", "");
	If(Contains(arra, curbin),
		x[4] = Char(arra[curbin]);
		:Bins[rows[i]] = Concat Items(x);
	,
		continue();
	);
);
-Jarmo
Jackie_
Level VI

Re: Replace characters in the string

Thanks a lot Jarmo