Returning a string from a piecewise function

I need to define a function that behaves as a piecewise function, but will return a string instead of a value. Part of the function that I have written is provided below. The full function continues for several additional lines with additional values of od and different values for the slot positions. The line of code for od == 1.050 illustrates where I need to return the string "XXS". The values od and tol are input values to the notebook. The notebook calculates T.

pipeschedule[od_, tol_, T_] :=
Which[
od == 0.405, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.068, 80, 0.095],
od == 0.540, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.088, 80, 0.119],
od == 0.675, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.091, 80, 0.126],
od == 0.840, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.091, 80, 0.126],
od == 1.050, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}, {#5, (tol*#4) < T <= (tol*#6)}, {#7, (tol*#6) < T <= (tol*#8)}}, "___"]&[40,0.109, 80, 0.147, 160, 0.188, "XXS", 0.294]]]


Is there a different function that will accept strings?

• Hi bangeo, welcome to the Mathematica Stack Exchange. Can you give an example of a cases where you have strings? It's hard to envision what you mean to do with the function. In any case, it sounds like you're looking for If or Switch? – Carl Lange Apr 20 '19 at 21:58
• Thanks for the help and example of where I have strings from the larges code is pipeschedule[od_, tol_, T_] := Which[ od == 0.405, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, ""] &[40, 0.068, 80, 0.095], od == 1.050, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}, {#5, (tol*#4) < T <= (tol*#6)}, {#7, (tol*#6) < T <= (tol*#8)}}, ""] &[40, 0.109, 80, 0.147, 160, 0.188, "XXS", 0.294]]. In the final case "XXS" is the string. Thanks again for your help. – bangeo Apr 20 '19 at 23:13
• Please edit your question and add that information to it inside  characters so that it's formatted correctly (see formatting help near the edit box) - comments aren't useful for this type of thing! – Carl Lange Apr 20 '19 at 23:24
• I think I'm still not understanding what it is that you're trying to do. If I input pipeschedule[1.050, 0.1, 0.02], I get XXS as the result from your code. Is that not what you want to get? If you want to avoid using Piecewise, it still looks to me like Which should work just fine. – MassDefect Apr 20 '19 at 23:27
• Thanks for the response. Interesting that I do not get the same result. I will revise the question to show the result I receive. For pipeschedule[1.050, 1, .2], I get – bangeo Apr 23 '19 at 2:39

I usually use Which for these sorts of things, but it might depend on exactly what your needs are.

f[w_, x_, y_, z_] := Which[
tmin <= x tol,
w,
x tol < tmin <= z tol,
y]
f[40, 0.1, 80, 0.3]


You can also set it up as a pure function like you have in your question:

Which[
tmin <= #2 tol,
#1,
#2 tol < tmin <= #4 tol,
#3]&[40, 0.1, 80, 0.3]
`
• Thanks so much for the feedback. I see I need to provide additional detail. – bangeo Apr 20 '19 at 23:02
• Please see above comment for additional information. Thanks again – bangeo Apr 20 '19 at 23:15