Match string expression with bound variable

The following shell sessions show some behavior that I would like to understand:

1> A = "Some text". "Some text" 2> "Some " ++ R = A. "Some text" 3> R. "text" 4> B = "Some ". "Some " 5> B ++ L = A. * 1: illegal pattern 

Surely statements 2 and 5 are syntactically identical? I would like to use this idiom to extract some text from the line where B read from the configuration file. Is this possible, and what syntax should be used instead of what is shown in 5) above?

Thanks!

+4
source share
1 answer

The LHS ++ RHS template extends at compiletime to [ lhs0, lhs1, lhs2 | RHS] [ lhs0, lhs1, lhs2 | RHS] (where LHS =:= [lhs0, lhs1, lhs2] and the compiler refuses to do this for anything other than letter strings / lists. Theoretically, it can do this for variables, but it just doesn’t right now.

I think in your case you need to do:

 Prefix = read_from_config(), TestString = "Some test string", case lists:prefix(Prefix, TestString) of true -> %% remove prefix from target string lists:nthtail(length(Prefix), TestString); false -> different_prefix end. 
+4
source

All Articles