Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: weird escaping problem with StringPattern inside

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100883] Re: [mg100860] Re: weird escaping problem with StringPattern inside
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Wed, 17 Jun 2009 04:37:21 -0400 (EDT)
  • References: <h12ggp$edj$1@smc.vnet.net> <200906170152.VAA25905@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

When in doubt, use RuleDelayed:

"a" ~~ x_ ~~ "c" :> x

Bobby

On Tue, 16 Jun 2009 20:52:59 -0500, dh <dh at metrohm.com> wrote:

>
>
> Hi Roger,
>
> to me, the mystery is that your rule works at all. From the manual:
>
>
>
> "Symbols that occur as pattern names in lhs are treated as local to the
>
> rule. This is true when the symbols appear on the right-hand side of /;
>
> conditions in lhs, and when the symbols appear anywhere in rhs, even
>
> inside other scoping constructs. "
>
>
>
> The problem seems to be that Mathematica does not always localize
>
> pattern variables. It does it only when it thinks it necessary.
>
> Consider:
>
> Module[{pat, var = x},
>
>   pat = "a" ~~ x_ ~~ "c" -> x
>
>   ]
>
> this gives: "a" ~~ x_ ~~ "c" -> x
>
> on the other hand:
>
> Module[{pat, var = x},
>
>   pat = "a" ~~ x_ ~~ "c" -> var
>
>   ]
>
> gives: "a" ~~ x$_ ~~ "c" -> x
>
> this will not work, but it makes sense. Giving the variable name in a
>
> variable, it is a good guess to assume that the user does not mean the
>
> pattern variable. I could live with this. However, unfortunately it is
>
> not consistently implemented. Consider:
>
> var = x;
>
> pat = "a" ~~ x_ ~~ "c" -> var
>
> this gives: "a" ~~ x_ ~~ "c" -> x what is inconsistent compared to the
>
> foregoing.
>
>
>
> I consider this a bug. It would be better when it would work like
>
> described in the manual. I think you should inform Wolfram about this.
>
> Maybe somebody from the group has something to say about this.
>
> Daniel
>
>
>
>
>
> 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
>
>>
>
>
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: how to _paste_ hyperlink into notebook
  • Next by Date: Re: Avoid printing leading zero
  • Previous by thread: Re: weird escaping problem with StringPattern inside Module[]
  • Next by thread: Re: weird escaping problem with StringPattern inside