2
$\begingroup$
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?

$\endgroup$
3
  • $\begingroup$ Please use descriptive titles for your questions instead of "Little problem with X" $\endgroup$ Commented Jun 14, 2013 at 5:12
  • $\begingroup$ 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. $\endgroup$ Commented Jun 14, 2013 at 5:47
  • $\begingroup$ @Mr.Wizard ok, no problem. $\endgroup$ Commented Jun 14, 2013 at 5:56

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ nice+1, I mush have tried RuleDelayed incompletely. :( $\endgroup$ Commented Jun 14, 2013 at 4:53
  • $\begingroup$ How do you think my formatting here? Actually I like MrWizard's style one yellow blockquotes for output. mathematica.stackexchange.com/questions/26974/… $\endgroup$ Commented Jun 14, 2013 at 5:11
  • $\begingroup$ @HyperGroups I don't really know, I like Leonid Shifrin's style, with codes formatted neatly. $\endgroup$ Commented Jun 14, 2013 at 5:24
  • $\begingroup$ 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 $\endgroup$ Commented Jun 14, 2013 at 5:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.