Re: weird escaping problem with StringPattern inside Module[]
- To: mathgroup at smc.vnet.net
- Subject: [mg100810] Re: weird escaping problem with StringPattern inside Module[]
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Sun, 14 Jun 2009 21:19:55 -0400 (EDT)
- References: <h12ggp$edj$1@smc.vnet.net>
Hi,
try
getWeather[inputStr_String] :=
Block[{parseVars, parsePattern}, parseVars = {abc, def};
parsePattern =
"the " ~~ abc__ ~~ " in " ~~ def__ ~~ " stays main" -> parseVars;
Print["patt=", parsePattern];
Flatten@StringCases[inputStr, parsePattern]]
Regards
Jens
divisor wrote:
> Hello MathGroup:
>
> I am having a strange problem where my code works fine in a Notebook
> session but acts strangely inside a Module[]. I presume that this is
> some scoping problem that is currently beyond me.
>
> I know how Module[] takes local variables and adds "$nnn" or something
> to the variable. It is doing a similar thing to my string pattern and
> I have no idea how to "escape" this and disable this behavior.
>
> Any help you can provide is greatly appreciated.
>
> This code works as expected:
>
> parseVars = {abc, def};
> text0 = "the rain in spain stays mainly...";
> parsePattern = "the " ~~ abc__ ~~ " in " ~~ def__ ~~ " stays main" ->
> parseVars;
> Flatten@StringCases[text0, parsePattern]
>> {"rain", "spain"}
> Print["patt=", parsePattern]
>> patt=the ~~abc__~~ in ~~def__~~ stays main->{abc,def}
>
>
> This is the module containing the exact same logic:
>
> getWeather[inputStr_String] := Module[
> {parseVars, parsePattern}
> , parseVars = {abc, def};
> parsePattern = "the " ~~ abc__ ~~ " in " ~~ def__ ~~ " stays main" -
>> parseVars;
> Print["patt=", parsePattern];
> Flatten@StringCases[inputStr, parsePattern]
> ]
>
> This shows the symptom. Notice how the pattern vars are suffixed and
> hence do not match those in the rhs of StringCases[] with the result
> being all are "no match"!?!?!
>
> getWeather[text0]
>> patt=the ~~abc$__~~ in ~~def$__~~ stays main->{abc,def}
>> {abc, def}
>
> TIA.
>
> Regards..
>
> Roger Williams
> Franklin Laboratory
>