2
$\begingroup$

I know how to do a Check for the list in general:

lst = {"0/0", "1/25", "1/36", "1/49"}

Quiet[Check[StringReplace[lst, "0/0" -> Missing["Unavailable"]] // ToExpression, _, ToExpression::notstrbox], ToExpression::notstrbox]

But I need to to the check that for each element, so to have a result like:

{Missing["Unavailable"], 1/25, 1/36, 1/49}

Any idea? Thanks!

$\endgroup$
8
  • $\begingroup$ Why not simply lst /. {"0/0" :> Missing["Unavailable"]} ? $\endgroup$
    – Mr.Wizard
    Apr 15, 2015 at 13:46
  • 1
    $\begingroup$ How about myfun[x_] := If[x == "0/0", Missing["Unavailable"], ToExpression[x]] $\endgroup$ Apr 15, 2015 at 13:51
  • 1
    $\begingroup$ @Mr.Wizard I need to do ToExpression after $\endgroup$
    – SuTron
    Apr 15, 2015 at 13:53
  • $\begingroup$ @Mr.Wizard I think he wants numerical out puts as 1/25 instead of "1/25". In your case, the replacement rule only replaces the "0/0" case, but does not convert the strings to numerical values? $\endgroup$ Apr 15, 2015 at 13:53
  • $\begingroup$ Sorry, I missed that part. Reopened. $\endgroup$
    – Mr.Wizard
    Apr 15, 2015 at 13:54

3 Answers 3

3
$\begingroup$

How about something like this?

myfun[x_] := If[x == "0/0", Missing["Unavailable"], ToExpression[x]]
Map[myfun, lst]

{Missing["Unavailable"], 1/25, 1/36, 1/49}

$\endgroup$
3
$\begingroup$

Possibly you would be pleased with an alternative approach:

lst = {"0/0", "1/25", "1/36", "1/49"};

Quiet @ ToExpression @ lst /. Indeterminate -> Missing["Unavailable"]
{Missing["Unavailable"], 1/25, 1/36, 1/49}
$\endgroup$
1
$\begingroup$
lst = {"0/0", "1/25", "1/36", "1/49"};

Temporarily change the definition of Indeterminate to Missing["NotAvailable"]:

Block[{Indeterminate = Missing["NotAvailable"]}, Quiet@ToExpression@lst]

{Missing["NotAvailable"], 1/25, 1/36, 1/49}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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