<div class="gmail_quote">On Mon, Jun 27, 2011 at 9:34 PM, Richard O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz">ok@cs.otago.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
On 28/06/2011, at 3:52 AM, Daniel Dormont wrote:<br>
<br>
> Suppose I want to test that a certain string String1 ends with a certain other string String2. The best I could come up with is:<br>
><br>
> case string:substr(lists:reverse(String1), 1, length(String2)) of<br>
>       String2 -> ok;<br>
>       _ -> not_ok<br>
> end<br>
><br>
> I was thinking there must be a better way, but various attempts along the lines of<br>
>       case String1 of<br>
>               _ ++ String2 -> ok;<br>
><br>
> all returned "illegal pattern" errors. I couldn't find a clear explanation online of when exactly ++ is allowed in a pattern and when it's not, so I'm a bit stumped.<br>
<br>
</div>Section 7.4 of the reference manual is the place where it says ++ is allowed<br>
as a pattern, and the only thing it allows on the left of ++ is an explicit<br>
string literal.  A pattern like<br>
        "foo"++X<br>
is just shorthand for<br>
        [$f,$o,$o|X]<br>
<br></blockquote><div><br>I see. I'd found that section in my searching earlier, but hadn't made the connection that it has to be literal (in my defense, the manual doesn't exactly say that). <br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

If you want to check whether String1 ends with String2,<br>
(a) you're going to need an even number of calls to lists:reverse/1<br>
(b) you are going to be kicking yourself that you didn't read the<br>
    documentation for the 'lists' module<br>
        <a href="http://www.erlang.org/doc/man/lists.html" target="_blank">http://www.erlang.org/doc/man/lists.html</a><br>
    because<br>
        lists:suffix(String2, String1)<br>
    does exactly what you want (modulo returning 'true' or 'false').<br></blockquote><div><br>Sometimes the challenge is not knowing where to look. I've used lists of course, but Erlang's lists is a pretty big module; it's easy to miss a few things here and there :)<br>
<br>Anyway, thanks.<br><br>dan <br></div></div><br>