2
$\begingroup$

This is closely related to the question here, the key difference is that here data is structured as nested Association rather than a flat list:

data2 = <|"1" -> <|"a" -> "hey now"|>, "2" -> <|"a" -> Missing[]|>|> // 
  Dataset;

enter image description here

data2  // Select[Quiet @StringMatchQ[#a, "hey*"] &] // Normal

<|"1" -> <|"a" -> "hey now"|>|>

Note, this will fail without the use of Quiet to suppress a Failure due to the match on Missing. And fails anyway as a Query:

data2[ Select[Quiet [StringMatchQ[#a, "hey*"] &]]]

Missing["Failed"]

The typical workaround: data2 // Normal // Dataset does not help here.

$\endgroup$
4
  • 3
    $\begingroup$ Isn't the issue entirely that you want StringMatchQ to work with non-Strings? $\endgroup$
    – chuy
    May 11, 2015 at 19:58
  • $\begingroup$ You can use either data2[Select[StringMatchQ[ToString@#a, "hey*"] &]] or the wordier data2[Select[ Length[Cases[{#a}, x_String /; StringMatchQ[x, "hey*"] ]] > 0 &]] if you feel the need to avoid ToString $\endgroup$
    – chuy
    May 11, 2015 at 20:18
  • $\begingroup$ As intented, StringMatchQ does not match Missing[] values, and its warning is suppressed by Quiet though somehow not as a Query. $\endgroup$ May 11, 2015 at 20:28
  • $\begingroup$ @alancalvitti The last comment I left in your previous post should help data2[ Select[Function[If[Head[#a] == String, StringMatchQ[#a, "hey*"]]]]] $\endgroup$
    – Hans
    May 11, 2015 at 20:34

2 Answers 2

1
$\begingroup$

The simplest "workaround" I've found to prevent your query from aborting is to set properly the Query option FailureAction which is indeed by default set to "Abort" ! ;)

Given your data :

myDataset = <|"1" -> <|"a" -> "hey now"|>, "2" -> <|"a" -> Missing[]|>|> // Dataset;
myQuery = Select[StringMatchQ[#a, "hey*"] &];

the default behavior is to simply abort with a Failure message:

myDataset[myQuery]

enter image description here

unless you set :

myDataset[myQuery, FailureAction -> None]

enter image description here

The query did not abort, the result is correct but a warning message was printed. To suppress the message you can as before simply wrap the command in Quiet.

Remarks

1/ Regarding this FailureAction option, I thought first that the solution would be to set it rather to "Drop" but the query returns Dataset``$DropPlaceholder ... Not sure what happens here ...

2/ There is also another very interesting query option: MissingBehavior which I thought was even more suited here. Not sure again why it does not work either.

3/ Why (as OP asked) does Select[Quiet@StringMatchQ[#a, "hey*"] &][myDataset] return the correct result without Failure, whereas myDataset[Select[Quiet@StringMatchQ[#a, "hey*"] &]] abort ?? It seems that this second form is actually working in version 10.0 according to @Hans answer ? Anyone to confirm ? (At the time of this answer I'm using v10.2.)

$\endgroup$
0
$\begingroup$

Does this help

data2[ Select[Function[If[Head[#a] == String, StringMatchQ[#a, "hey*"]]]]] 

Then can you try

    data2[
 Select[Function[Quiet[StringMatchQ[#a, "hey*"]]]]]

$Version
10.0 for Microsoft Windows (64-bit) (June 29, 2014)

Returned same as the If[]

Or re-written as

    data2[
 Select[(Quiet[StringMatchQ[#a, "hey*"]]) &]]

Note the placement of ().

$\endgroup$
1
  • $\begingroup$ No, the focus of this q is on Query's behavior with respect to Quiet $\endgroup$ May 11, 2015 at 20:51

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.