cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
sophiaw
Level III

Splitting a String of Characters

Hi! I want to parse a string of characters. I have 202 binary bits in my string and I want to cut that string in half. I believe I could find the length of the string and maybe get it to return the values at a certain number of characters. I'm just not sure how to actually use that to cut it in half and have two columns with 101 bits in each. And after I get that, I want to separate every bit into it's own column. So I want 101 columns of either a '1' or a '0' in it. I just don't know how to parse it by number of characters.

Thanks for any help!!

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Splitting a String of Characters

Try this


// input data is a string of character 1s and 0s representing bits

string = "11110000111000110010";

// split the string in two at the middle

// this substring starts at character 1 and goes for half the length

firsthalf = Substr( string, 1, Length( string ) / 2 );

// this substring starts just beyond the previous and the

// implicit length goes to the end

secondhalf = Substr( string, 1 + Length( string ) / 2 );

// the 1 and 0 bits go one per column

ncols = Length( firsthalf );

// start a fresh table

dt = New Table( "example" );

// add columns wtih names like "c3" for each bit

For( i = 1, i <= ncols, i++, // the || operator joins two strings

  dt << New Column( "c" || Char( i ) ) // i is a number, char makes a string

);

// make a helper function to add bits to a row in dt

adddata = Function( {bits}, // parameter to function

  {i, c}, // local variables

  dt << addrows( 1 ); // function knows to use dt

  For( i = 1, i <= Length( bits ), i++,

  c = Column( dt, "c" || Char( i ) ); // same as above

  dt:c = Num( Substr( bits, i, 1 ) ); // put bit in column

  );

);

// each call to adddata puts a row of bits in dt

adddata( firsthalf );

adddata( secondhalf );

adddata( "1010101010" );

adddata( "1111111111" );

adddata( "0000000000" );


10827_pastedImage_4.png

Craige

View solution in original post

3 REPLIES 3
Craige_Hales
Super User

Re: Splitting a String of Characters

Try this


// input data is a string of character 1s and 0s representing bits

string = "11110000111000110010";

// split the string in two at the middle

// this substring starts at character 1 and goes for half the length

firsthalf = Substr( string, 1, Length( string ) / 2 );

// this substring starts just beyond the previous and the

// implicit length goes to the end

secondhalf = Substr( string, 1 + Length( string ) / 2 );

// the 1 and 0 bits go one per column

ncols = Length( firsthalf );

// start a fresh table

dt = New Table( "example" );

// add columns wtih names like "c3" for each bit

For( i = 1, i <= ncols, i++, // the || operator joins two strings

  dt << New Column( "c" || Char( i ) ) // i is a number, char makes a string

);

// make a helper function to add bits to a row in dt

adddata = Function( {bits}, // parameter to function

  {i, c}, // local variables

  dt << addrows( 1 ); // function knows to use dt

  For( i = 1, i <= Length( bits ), i++,

  c = Column( dt, "c" || Char( i ) ); // same as above

  dt:c = Num( Substr( bits, i, 1 ) ); // put bit in column

  );

);

// each call to adddata puts a row of bits in dt

adddata( firsthalf );

adddata( secondhalf );

adddata( "1010101010" );

adddata( "1111111111" );

adddata( "0000000000" );


10827_pastedImage_4.png

Craige
sophiaw
Level III

Re: Splitting a String of Characters

Thank you! That helped a lot. Unfortunately that was just the beginning of what I wanted to do, and I'm not sure how to do that rest...

So I really need to indicate what half of the string I'm looking at. All 100 rows of strings I am splitting are for the same data, and all the first half strings have the same name, and so do the second half strings. And once I have that I need to graph both halves separately. When I tried to graph it manually in JMP, it did not turn out as I expected. How I have done this analysis is through Excel because it displayed the graph how I wanted it to be. Basically I just need all the new columns that the bits go into (in this case they are voltages) along the x-axis and the data along the y-axis

The only problem is that I couldn't figure out how to do that in JMP. Any help?

Thank you so much!!

ian_jmp
Staff

Re: Splitting a String of Characters

Generally there is a dependency between the way your data is organised and what a JMP platform will (or won't) give you.

I'm not sure I've understood what you are after completely, but if you add this code to Craige's above I think you will get close:

dt2 = dt << Transpose(

columns( :c1, :c2, :c3, :c4, :c5, :c6, :c7, :c8, :c9, :c10 ),

Output Table( "Transpose of example" )

);

dt2 << Graph Builder(

Show Control Panel( 0 ),

Variables(

X( :Label ),

Y( :Row 1 ),

Y( :Row 2 ),

Y( :Row 3 ),

Y( :Row 4 ),

Y( :Row 5 )

),

Elements( Position( 1, 1 ), Bar( X, Y, Legend( 12 ) ) ),

Elements( Position( 1, 2 ), Bar( X, Y, Legend( 13 ) ) ),

Elements( Position( 1, 3 ), Bar( X, Y, Legend( 15 ) ) ),

Elements( Position( 1, 4 ), Bar( X, Y, Legend( 23 ) ) ),

Elements( Position( 1, 5 ), Bar( X, Y, Legend( 27 ) ) )

);

You would have to adjust the values in column 'Label' to be the voltages, and you could do that with 'Recode' or through a little more JSL. Once you have what you want, it may be that there is a more direct way to get there.