cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Get the free JMP Student Edition for qualified students and instructors at degree granting institutions.
Choose Language Hide Translation Bar
View Original Published Thread

How to convert a numeric list into a character one?

anne_sa
Level VI

Hi everybody,

I have a list containing numbers and I would like to transform them into characters. Is there a way to do that without using a loop? I was not able to find the proper function.

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager


Re: How to convert a numeric list into a character one?

New in JMP 16 is the For Each() function which iterates over a container – list, matrix, or associative array – to perform an operation on each element.

 

Here is an example to show how to use it to convert your list of numbers to a list of character strings.

 

Names Default To Here( 1 );
theList = {7, 8, 9};

For Each( {value, index}, theList, theList[index] = Char( value ) );

 

 

-Jeff

View solution in original post

13 REPLIES 13
txnelson
Super User


Re: How to convert a numeric list into a character one?

Here is a little script that creates a function that does what you want.  You can either define and use the function in your code, or use the 4 statements in open code to do the conversion.

Names Default To Here( 1 );
theList = {7,8,9};

NumListToChar = Function( {myList},
	{defaultlocal},
	dtx = New Table( "x", New Column( "y", values( myList ) ), private );
	dtx:y << data type( character );
	myList = dtx:y << get values;
	Close( dtx, nosave );
	Return( myList );
);

theList = NumListToChar( theList );
Jim
anne_sa
Level VI


Re: How to convert a numeric list into a character one?

Thanks for your answer @txnelson it is indeed what I was looking for.

I just wonder what is the faster between a loop and a function which involves table creation. Any idea?

Jeff_Perkinson
Community Manager Community Manager


Re: How to convert a numeric list into a character one?

New in JMP 16 is the For Each() function which iterates over a container – list, matrix, or associative array – to perform an operation on each element.

 

Here is an example to show how to use it to convert your list of numbers to a list of character strings.

 

Names Default To Here( 1 );
theList = {7, 8, 9};

For Each( {value, index}, theList, theList[index] = Char( value ) );

 

 

-Jeff
anne_sa
Level VI


Re: How to convert a numeric list into a character one?

Thanks @Jeff_Perkinson !

I did some simulations just by curiosity to compare the execution time of each solution and it seems that the for each function is on average the fastest one!

So I am gonna pick that one although the function of @txnelson also works very well.

 

Thanks to both of you

Jeff_Perkinson
Community Manager Community Manager


Re: How to convert a numeric list into a character one?

Here's a quick timing program for the three methods, for loop, for each, and custom function using data table.

 

Names Default To Here( 1 );

listsize=1000000;

theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

NumListToChar = Function( {myList},
	{defaultlocal},
	dtx = New Table( "x", New Column( "y", values( myList ) ), private );
	dtx:y << data type( character );
	myList = dtx:y << get values;
	Close( dtx, nosave );
	Return( myList );
);

start = HP Time();
For Each( {value, index}, theList, theList[index] = Char( value ) );
foreachloop = HP Time() - start;

theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

start = HP Time();
theList = NumListToChar( theList );
customfunction = HP Time() - start;

theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

start = HP Time();
For( i = 1, i <= N Items( theList ), i++,
	theList[i] = Char( theList[i] )
);
forloop = HP Time() - start;

show(foreachloop, customfunction, forloop);

For me with the listsize shown above the results are very similar between the three methods:

 

foreachloop = 1106974;
customfunction = 1077862;
forloop = 1400643;
-Jeff
Craige_Hales
Super User


Re: How to convert a numeric list into a character one?

Extending Jeff's answer

Different overheads hurt for small sizes and help for large sizes.Different overheads hurt for small sizes and help for large sizes.

dt = New Table( "Untitled 3",
	Add Rows( 0 ),
	New Column( "size", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
	New Column( "for", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
	New Column( "foreach", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
	New Column( "custom", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) )
);

NumListToChar = Function( {myList},
	{defaultlocal},
	dtx = New Table( "x", New Column( "y", values( myList ) ), private );
	dtx:y << data type( character );
	myList = dtx:y << get values;
	Close( dtx, nosave );
	Return( myList );
);

For( listsize = 1, listsize < 1e7, listsize *= 2, 

	theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

	start = HP Time();
	For Each( {value, index}, theList, theList[index] = Char( value ) );
	foreachloop = HP Time() - start;

	theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

	start = HP Time();
	theList = NumListToChar( theList );
	customfunction = HP Time() - start;

	theList = As List( J( listsize, 1, Random Integer( 100 ) ) );

	start = HP Time();
	For( i = 1, i <= N Items( theList ), i++,
		theList[i] = Char( theList[i] )
	);
	forloop = HP Time() - start;

	Show( foreachloop, customfunction, forloop );
	dt << addrows( 1 );
	dt:size[N Rows( dt )] = listsize;
	dt:for[N Rows( dt )] = forloop / 1e6;
	dt:foreach[N Rows( dt )] = foreachloop / 1e6;
	dt:custom[N Rows( dt )] = customfunction / 1e6;
	Wait( 0.1 ); // so it shows on display
);
Craige
anne_sa
Level VI


Re: How to convert a numeric list into a character one?

Thanks for the additional information @Jeff_Perkinson and @Craige_Hales !

We always learn a lot of things with the Community!!

ms
Super User (Alumni) ms
Super User (Alumni)


Re: How to convert a numeric list into a character one?

Nice illustration of the size-dependent performance of the different functions. I'm still using JMP 15 and can't use For Each() yet, but to extend Jeff's timing exercise further I toss in two more functions that give the same result. 

Repeat(k = 0; {k++; Char( theList[k] )}, listsize);

Words( Substr( Char( theList ), 2, Length( Char( thelist ) ) - 2 ), ", " );

NumtoChar.png

The For loop, which is the fastest for small lists, is surpassed by Words at size>50, by the data-table-detour function at size>1000 and by Repeat at size>30000.

I was a little surprised to see that Words remained the fastest for size>50, about twice as fast at millions of "words".

anne_sa
Level VI


Re: How to convert a numeric list into a character one?

Wow I was looking for one solution and I got so many! JMP is amazing! Thanks all of you!