1
$\begingroup$

This question already has an answer here:

I have two lists, one for the length of words and another list for the corresponding bonus points.

play = {"a", "aa", "caa", "ccbaa", "ccxxxcaa", "ccccaa", "cccccaa",   "ccccccaa", "sdf"}  
len = StringLength[play]  
{1, 2, 3, 5, 8, 6, 7, 8, 4}  
bonusPoints = {0, 10, 20, 30, 40, 50, 60, 75, 85, 95, 100}  
(* Excel-like lookup function [len, bonusPoints] *)  
(* 0, 10, 20, 40, 75, 50, 60, 75, 30 *) 

I have tried with Thread and Lookup but get various errors ("different lengths", etc.). Funny that there is no other question about Excel lookup look alike, it is such a marvellous function. Thank you for your time!

$\endgroup$

marked as duplicate by user9660, RunnyKine, MarcoB, m_goldberg, C. E. Mar 24 '16 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ bonusPoints[[len]] ? ( If your expected result is correct maybe it needs more explination ) $\endgroup$ – george2079 Mar 23 '16 at 21:18
  • $\begingroup$ Well, the line Excel like lookup function will somehow take len and bonusPoints as parameters. That is what I mean. Or have I misunderstood your comments? $\endgroup$ – JSP Mar 23 '16 at 21:21
  • $\begingroup$ In addition to what @george2079 said, in more general case that you might encounter non-integers, maybe even Interpolation[bonusPoints]? Then, simply Map[Interpolation[bonusPoints], len] $\endgroup$ – MathX Mar 23 '16 at 21:22
  • $\begingroup$ I am sorry, I have corrected the desired output. There are no numbers in between, only integers. $\endgroup$ – JSP Mar 23 '16 at 21:42
  • $\begingroup$ Could you please elaborate what you want the answer to be? As @george2079 said bonusPoints[[len]] does return what you want which is {0, 10, 20, 40, 75, 50, 60, 75, 20}. What is wrong with that? Since you edited you changed the length of the last string so I guess you want the answer's last element to be 20? $\endgroup$ – MathX Mar 23 '16 at 21:59
3
$\begingroup$

Building an association is a good tool for tackling this problem because it provides a solution that will be similar to way you would do it in Excel.

bonusPoints = {0, 10, 20, 30, 40, 50, 60, 75, 85, 95, 100};
assoc = AssociationThread[Range[Length[bonusPoints]] -> bonusPoints];
Lookup[assoc, {1, 2, 3, 5, 8, 6, 7, 8, 4}]
{0, 10, 20, 40, 75, 50, 60, 75, 30}
$\endgroup$
  • $\begingroup$ The answer with Associations is really pedagogical and solves the problem in an elegant manner. Thank you @m_goldberg. $\endgroup$ – JSP Mar 27 '16 at 6:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.