I'm trying to take a list of the form { {"65 + 3 months", 75}, {"65 + 4 months", 75.1} }
, and transform the string part to a number such as 65.25 or 65.333, respectively.
What I really want to do is something like:
list = { {"65 + 3 months", 75}, {"65 + 4 months", 75.1} };
list /. {y:NumberString~~" + "~~m:NumberString,p_}:>{y+m/12,p}
That form doesn't work, I believe, because I need to use StringReplace to cause the StringExpression
to do any matching.
So since I can't make that work, I tried something like:
list /.{ys_String, p_} :>
{StringReplace[ys, y:NumberString~~" + "~~m:NumberString~~" months":>y+m/12], p}
But in that case, I'm getting { { StringExpression[3/12 + 65], 75}, {StringExpression[4/12 + 65], 75.1} }
, and have no idea where the StringExpression
heads came from in that case or what to do with them without making my expression uglier and even further from just describing the patterns that I'm trying to transform from/to.
Is there a simple way to compose a pattern that does some string matching and some structure matching? If not, what's the best way to write the StringReplace
version for the problem above.