MathGroup Archive 2011

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

Search the Archive

Re: Difficulty with patterns

On 10/23/11 at 6:24 AM, Paul.McHale at (McHale, Paul)

>Example 1 uses my old standby, the Table[] function.  Works fine.  I
>am attempting to get better with patterns, so I tried example 2 and
>3.  They work, but gives several errors about use of stringjoin
>needing strings.  What am I doing wrong?

>Error: StringJoin::string: String expected at position 1 in

>(* Load string from WolframAlpha  *) m


>(* example 2 *)
>mNew=Cases[m, {{n_String,o_String,p_String},q_} -> { n <>  ", " <> o  <> ", " <>  p,q},Infinity ];

The problem with the above occurs when the replacement rule is
evaluated. It is evaluated immediately, At that time, n, o, and
p are symbols without assigned values. So, the effect is to
evaluate n <> ", " before n is defined to be a string which
produces the error message. You can avoid this problem by using
RuleDelayed (:>) instead of Rule (->). That is:

mNew=Cases[m, {{n_String,o_String,p_String},q_} :> { n <>  ", "
<> o  <> ", " <>  p,q},Infinity ];

Will do what you want without the error message. For what it is
worth, I would have done

mNew = m[[1, 2 ;;]]/.{a_List, b_Integer}:>{StringJoin@@Riffle[a,
{", ",", "}], b};

to get the desired output

  • Prev by Date: Re: Creating Objects
  • Next by Date: Re: Full simplify problem
  • Previous by thread: Re: Difficulty with patterns
  • Next by thread: Re: Difficulty with patterns