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
Jackie_
Level VI

Extract numbers from string

Hi,

 

I want to extract all the numbers contain in [] and store in the list. Is there a way to achieve this with jsl?

 

I would like to extract numbers from the column and store in the list which should look something like this:

 

bins = {"52","79","39 41 43", "38 40 42", "1"};

Any advice would be much helpful.

Jacksmith12_0-1670537130464.png

Thanks

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Extract numbers from string

Here is a simple example that reads in your sample data table and produces a list with the values

x = {"52", "79", "39", "41", "43", "38", "40", "42", "1"};
Names Default To Here( 1 );
dt = current data table();
x = {};
For Each Row(
	If( :values != "",
		y = Eval( Parse( :values ) );
		For Each( {value}, y, Insert Into( x, Char( value ) ) );
	)
);

Show( x );
Jim

View solution in original post

Jeff_Perkinson
Community Manager Community Manager

Re: Extract numbers from string

There are a couple of different ways to do this.

 

You can get the values for your Values column into a list using the <<Get Values() message to the column. 

 

You'll need to get rid of the missing values from that list. It's easy to do this by locating the missing rows in the data table with Get Rows Where(). Then the Remove From() function will remove those elements from the list.

 

Then use For Each() and the Word() function to get rid of the square brackets (i.e., "[]").

 

dt = Data Table( "Sample.jmp" );

//locate the missing values
missings = dt << get rows where( Is Missing( :values ) );

//get a list of the Values column
lst = :Values << get values;

//Remove the missings from the list
Remove From( lst, As List( missings ) );

//strip the square brackets
For Each( {item, i}, lst, lst[i] = Word( 1, item, "[]" ) );

You could also do all this in the data table by adding a new formula column with the Word() function to strip the square brackets and then getting the values from the new column.

dt=Data Table("Sample.jmp");
new_col = dt << New Column( "New Values", Character,
	formula( Word( 1, :Values, "[]" ) )
);
lst = (new_col << get values)[dt <<
get rows where( !Is Missing( :New Values ) )];

 The Word() function is my favorite: If you learn only one Formula Editor function, Word() is the one 

-Jeff

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: Extract numbers from string

Here is a simple example that reads in your sample data table and produces a list with the values

x = {"52", "79", "39", "41", "43", "38", "40", "42", "1"};
Names Default To Here( 1 );
dt = current data table();
x = {};
For Each Row(
	If( :values != "",
		y = Eval( Parse( :values ) );
		For Each( {value}, y, Insert Into( x, Char( value ) ) );
	)
);

Show( x );
Jim
Jackie_
Level VI

Re: Extract numbers from string

Thanks Jim

Jeff_Perkinson
Community Manager Community Manager

Re: Extract numbers from string

There are a couple of different ways to do this.

 

You can get the values for your Values column into a list using the <<Get Values() message to the column. 

 

You'll need to get rid of the missing values from that list. It's easy to do this by locating the missing rows in the data table with Get Rows Where(). Then the Remove From() function will remove those elements from the list.

 

Then use For Each() and the Word() function to get rid of the square brackets (i.e., "[]").

 

dt = Data Table( "Sample.jmp" );

//locate the missing values
missings = dt << get rows where( Is Missing( :values ) );

//get a list of the Values column
lst = :Values << get values;

//Remove the missings from the list
Remove From( lst, As List( missings ) );

//strip the square brackets
For Each( {item, i}, lst, lst[i] = Word( 1, item, "[]" ) );

You could also do all this in the data table by adding a new formula column with the Word() function to strip the square brackets and then getting the values from the new column.

dt=Data Table("Sample.jmp");
new_col = dt << New Column( "New Values", Character,
	formula( Word( 1, :Values, "[]" ) )
);
lst = (new_col << get values)[dt <<
get rows where( !Is Missing( :New Values ) )];

 The Word() function is my favorite: If you learn only one Formula Editor function, Word() is the one 

-Jeff

Re: Extract numbers from string

Here is another approach. There is a lot of capability for processing strings, lists, and data columns in JMP!

 

Names Default to Here( 1 );'

// create sample
dt = New Table( "Sample",
	New Column( "Values", "Character", "Nominal",
		Values( { "", "", "[52]", "[79]", "[39 41 43]", "[38 40 42]", "", "", "", "[1]" } )
	)
);

// use sample
val = :Values << Get Values;

// find 
bins = List();

For( i = 1, i <= N Items( val ), i++,
	If( val[i] != "",
		Insert Into( bins,
			Substitute( val[i],
				"[", "",
				"]", ""
			);
		);
	);
);

Show( bins );