# message about string expected in pattern [duplicate]

names=Select[(StringCases[list2=Names["System*"],RegularExpression["[a-z]+"],IgnoreCase->True]//Flatten),MemberQ[list2,#]&];

namesReplaceRules=names/.x_String->(x->""<>x<>"");


I found it will generate the message, but also gives the right result in this case.

StringJoin::string: String expected at position 2 in <>x<>. >>

I can Use Quiet or Ignore the message, or Add one ToString to x

namesReplaceRules=names/.x_String->(x->""<>x<>"");


Which is the right choice in such a case?

More elegant codes?

-

## marked as duplicate by Mr.Wizard♦Jun 14 '13 at 5:46

Please use descriptive titles for your questions instead of "Little problem with X" –  rm -rf Jun 14 '13 at 5:12
I have closed this question as a duplicate. That question gets into a deeper issue of infinite evaluation but up front the problem is the same, use of Rule rather than RuleDelayed, and it is even in the context of String operations. If you disagree with this closure please respond to this comment. –  Mr.Wizard Jun 14 '13 at 5:47
@Mr.Wizard ok, no problem. –  HyperGroups Jun 14 '13 at 5:56

Well, I think this problem is related to the evaluation process of Rule. When performing a replacement with a named pattern (x_String in this case), you'd better use RuleDelayed rather than Rule:

names = Select[(StringCases[list2 = Names["System*"],
RegularExpression["[a-z]+"], IgnoreCase -> True] // Flatten),
MemberQ[list2, #] &];

namesReplaceRules = names /. x_String :> (x -> "" <> x <> "");


Note that even if you use Rule, you can still get the right result, this is because x -> "" <> x <> "" will evaluate to itself with a error message. But if the rhs of Rule evaluate to something else, something bad would happen.

-
nice+1, I mush have tried RuleDelayed incompletely. :( –  HyperGroups Jun 14 '13 at 4:53
How do you think my formatting here? Actually I like MrWizard's style one yellow blockquotes for output. mathematica.stackexchange.com/questions/26974/… –  HyperGroups Jun 14 '13 at 5:11
@HyperGroups I don't really know, I like Leonid Shifrin's style, with codes formatted neatly. –  mm.Jang Jun 14 '13 at 5:24
en, I learned that (newline ==> newline),fine. I added the input numbers for indicating possible Picture results, because rm-rf said that the image uploader would not be fixed in a short time. So I combine several picture results in one results to insert in the post more convinently. mathematica.stackexchange.com/a/26932/6648 –  HyperGroups Jun 14 '13 at 5:34