Message Boards Message Boards

0
|
5834 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Simple rewrite rule using String function works but shows error?

Posted 7 years ago

I used Matlab/Mathematica during my PhD in 1993 and now trying to learn the fascinating Wolfram Language. So this might be a newby question so bear with me. The below simple expression

{"a.del", "b.del", "c.del"} /. s__ -> StringDelete[s, ".del"]

generates the expectecd content

{"a", "b", "c"}

but generates also the following error

StringDelete::strse: String or list of strings expected at position 1 in StringDelete[s,.del].

What am I doing wrong? Thanks for your help. Regards Markus

3 Replies

Notice that these also work:

In[1]:= StringDelete[".del"] /@ {"a.del","b.del","c.del"}
Out[1]= {a,b,c}

In[2]:= StringDelete[".del"][{"a.del","b.del","c.del"}]
Out[2]= {a,b,c}

In[3]:= StringDelete[{"a.del","b.del","c.del"},".del"]
Out[3]= {a,b,c}
POSTED BY: Gustavo Delfino
Posted 7 years ago

No error message is generated if RuleDelayed is used instead of Rule:

In[1]:= {"a.del", "b.del", "c.del"} /. s__ :> StringDelete[s, ".del"]

Out[1]= {"a", "b", "c"}
POSTED BY: Hans Milton

Your syntax is a little unusual, but it did work. The error message is coming from the rule, which gets evaluated before being applied. It is at the lowest level, the second part of

ReplaceAll[string, rule]

where the rule has its right hand side StringDelete[s,".del"] and s is just a symbol so it generates that message. But it stays unevaluated, and once it has a valid list of strings instead of s it works.

The way I would expect this to be entered would be something like

StringDelete[{"a.del", "b.del", "c.del"}, ".del"]
POSTED BY: Todd Rowland
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract