cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
anne_sa
Level VI

How to convert a numeric list into a character one?

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!

13 REPLIES 13
Craige_Hales
Super User

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

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

Thanks @Craige_Hales ! I like it. Let me see if I can work it into the next Challenge. Should we try to avoid any iteration or just For (and its derivatives)?  One way of avoiding iteration is through expression evaluation. If you're unfamiliar with this approach, check out @joseph_morgan 's Discovery tutorial, point 1. For the above problem, the function might look something like this:

convertNumberToCharacter = Function({inList},{i=1,outList={}},
	outList[1::N Items(inList)] = Expr(Char(inList[i++]));
	Eval List(outList);
);

/* You can check it out using the code below.
	tStart  = HP Time();
	alist   = As List(J(10000,1,Random Normal(1e6,1e5)));
	newList = convertNumberToCharacter(alist);
	Print((HP Time()-tStart)/1e6);
*/

I have tried this in a few of the Challenges and, while succinct, tends to take longer and is harder to understand (unless you are fairly familiar with expression handling).

 

Craige_Hales
Super User

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

@DonMcCormack The vanilla for-loop is both fastest, and slowest, depending on problem size. I'm really surprised @ms the words-based solution performs so well at large sizes. I really like the unexpected answers that showcase a feature in JMP like that. I'm a bit surprised for-each is faster than for, at some size. I like knowing that too.

 

I would not try to avoid anything. I like your current plan for a discussion, rather than a clear winner. Here are some discussion starter ideas:

 

  • If you had N lists to convert, and they all had M elements, how do N and M affect the code you would write? Consider not just speed, but correctness and ease of maintenance.
  • Which ideas seem best for memory constrained machines?
  • How would you show "NaN" rather than "."?
  • What would a general purpose routine look like? What would you call it if it was built in to JMP?

 

Craige
XanGregg
Staff

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

There's also Transform Each, which appears to be a little faster than For Each here.

	theList = Transform Each( {value}, theList, Char( value ) );