Show position of a letter as numberHello,
I would like to have the position of a letter in a text counted and shown as a number. I have only X as letters and want the number in a different column. Spaces should be counted as well.
text:
XX X X
output:
12 4 9
How can I do this?
Thanks for your reply
Data TableScriptingRe: Show position of a letter as numberHi,
Using this script as a formula column should do the job:
X_pos = {};
For( i = 1, i <= Length( :text ), i++,
If( Substr( :text, i, 1 ) == "X",
X_pos[i] = Char( i ),
X_pos[i] = " "
)
);
Concat Items( X_pos );Re: Show position of a letter as numberFor me Concat Items() seems to add additional space when no second argument is given, so you might have to use Concat Items( X_pos , "")Re: Show position of a letter as numberThank you for this solution. I have not work with JMP scripting yet. Is there an easy way to add this to the script of my table? Or build the same expression in the formula editor?
Re: Show position of a letter as numberYou should be able to create new column and copy and paste @HadleyMyers solution directly to formula editor (with the Contains Item() fix if required):

Edit: This expects the column to be named as "text"
Re: Show position of a letter as numberHi,
As @jthi replied, you can copy and paste the script above into the formula editor. I've included it as a formula column in the table below (copy this script and paste it in a New Script window to recreate the table), and saved the table to this post as well.
New Table( "Show position of letter example table",
Add Rows( 1 ),
New Column( "text",
Character,
"Nominal",
Set Values( {"XX X X"} ),
Set Display Width( 87 )
),
New Column( "Output",
Character,
"Nominal",
Formula(
X_pos = {};
For( i = 1, i <= Length( :text ), i++,
If( Substr( :text, i, 1 ) == "X",
X_pos[i] = Char( i ),
X_pos[i] = " "
)
);
Concat Items( X_pos, "" );
),
Set Selected,
Set Display Width( 107 )
)
)