MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: question on Cases with elements that might be strings


On Mar 13, 10:32 am, ramiro <ramiro.barran... at uvm.edu> wrote:
> Hi,
>
> The following is a valid pattern match:
>
> In[96]:= StringMatchQ["s1", StringExpression["s", NumberString]]
>
> Out[96]= True
>
> HOWEVER, the following does not work:
> In[89]:= Cases[{{1 -> "s1", 1}, {2 -> "s2", 2}}, {_ ->
> StringExpression["s", NumberString], _}]
>
> Out[89]= {}
>
> Even though this works:
> In[95]:= Cases[{{1 -> "s1", 1}, {2 -> "s", 2}}, {_ ->
> StringExpression["s"], _}]
>
> Out[95]= {{2 -> "s", 2}}
>
> How come cases does not work on ln[89]?
>
> Ramiro

StringExpression cannot be used with Cases.  Generally, string
patterns and expression patterns cannot be mixed.  In[95] doesn't
"work" either.  It returns the result you expected only because
StringExpression["s"] was immediately evaluated to "s".  So, in
In[85], change the StringExpression[ ... ] to s_String /;
StringMatchQ[s, "s" ~~ NumberString], and it'll work fine.


  • Prev by Date: Re: Path to *.m file
  • Next by Date: NDSolve problem
  • Previous by thread: Re: question on Cases with elements that might be strings
  • Next by thread: Need help to find position of max location