I have a dataset with missing or wrong values. A module should replace those values so that the dataset length is preserved. For identifying the positions to be replaced I use Position[]
.
Here is some data
data = {1, 2, 0, 4, , 6, "", 8} (* test set *);
criterList = {0, "", Null} (* selection criteria *);
Outside a Module
it works correctly like this
temp = Or @@ Map[u == # &, criterList];
Position[data, u_ /; Evaluate@temp]
{{3}, {5}, {7}}
The positions of values to be replaced are correctly identified.
However, inside a Module the same Code doesn't evaluate as above:
posMod[data_, criterList_] := Module[{u, res},
temp = Or @@ Map[u == # &, criterList];
Print[temp];
Print["a) ", Position[data, u_ /; Evaluate@temp]] (* Why doesnt it work? *);
Print["b) ", Position[data, u_ /; {u == 0 || u == "" || u == Null}]] (* temp evaluated *);
Print["c) ", Position[data, u_ /; Evaluate[Or @@ Map[u == # &, criterList]] ]] (*just copied *);
]
posMod[data, criterList]
a) {}
b) {}
c) {{3},{5},{7}}
Why does a) and b) not evaluate like c)?
Module[{u}, {u, u_, u_ /; u}]
returns what it returns. Aside of the main question, you can useReplace[data, Alternatives@@cri -> whatever, {1}]
$\endgroup$u_ /. u == 0 || u == "" || u == Null
instead ofu_ /; {u == 0 || u == "" || u == Null}
? $\endgroup$Position[data, Alternatives @@ criterList]
? $\endgroup$Block
instead ofModule
? $\endgroup$