Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5871

Re: Difficulty with patterns

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.64.80.MISMATCH!postnews.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeeds.sol.net!post2.nntp.sol.net!posts.news.twtelecom.net!nnrp2.twtelecom.net!not-for-mail
From A Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Difficulty with patterns
Date Tue, 25 Oct 2011 10:16:08 +0000 (UTC)
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Sender steve@smc.vnet.net
Approved Steven M. Christensen <steve@smc.vnet.net>, Moderator
Message-ID <j86298$5kf$1@smc.vnet.net> (permalink)
References <j80qbl$ae1$1@smc.vnet.net>
Lines 58
NNTP-Posting-Date 25 Oct 2011 09:19:53 GMT
NNTP-Posting-Host 1674c4ba.news.twtelecom.net
X-Trace DXC=D]6^l5^bQUI54`^_eAU6dKC_A=>8kQj6Mm^eQlV`f18GSNHYe;0_B=EEFiONJ7[GoFE?;6OLO47@I
X-Complaints-To abuse@twtelecom.net
Xref x330-a1.tempe.blueboxinc.net comp.soft-sys.math.mathematica:5871

Show key headers only | View raw


Hi,

> I have recently become increasingly uncomfortable with my areas of
> ignorance with Mathematica.  The Virtual conference has some really
> cool demos.  In one, a website is read in and a table of data
> extracted.  Without patterns, it would have been very difficult.
> Need a little help with an example.
>
> 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
> StringJoin[n].
>
> (* Load string from WolframAlpha  *) m
> WolframAlpha["usa",{{"LargestCities:CountryData",1},"ComputableData"},
>PodStates->
 > {"LargestCities:CountryData__More","LargestCities:CountryData__More"}
 > ];
>
> (* example 1 *) mNew=Flatten[m,1]; mNew=Drop[mNew,1];
> mNew=Drop[mNew,-1]; mNew=Table[{i[[1,1]]<>  i[[1,2]]<>  i[[1,2]]
> ,i[[2]]},{i,mNew}]; Grid[mNew,Frame->All]
>
> (* example 2 *) Clear[n,o,p,q]; mNew=Cases[m,
> {{n_String,o_String,p_String},q_} ->  { n<>   ", "<>  o<>  ", "<>
> p,q},Infinity ]; Grid[mNew,Frame->All]
>
> (* example 3 *) Clear[n,o,p,q]; mNew=Cases[m, {{n_,o_,p_},q_} ->  { n
> ,  ", ", o  , ", ", p,q},Infinity ]; mNew = mNew /. {n___,q_Integer}
> ->  {StringJoin[n],q}; Grid[mNew,Frame->All]
>
> I even tried, mNew = Flatten[m, 1]; mNew = Drop[mNew, 1]; mNew =
> Drop[mNew, -1]; mNew = Table[{Head[i[[1, 1]]], Head[i[[1, 2]]], Head[
> i[[1, 2]]] , i[[2]]}, {i, mNew}]; (*mNew=Table[{i[[1,1]]<>
> i[[1,2]]<>  i[[1,2]] ,i[[2]]},{i,mNew}];*) Grid[mNew, Frame ->  All]
>
> They are all strings!

that's not the problem, also you problem is not with patterns but with 
evaluation: when using -> (Rule) the RHS is evaluated before the pattern 
matching actually is started. The error message comes from 
string-joining the symbols n,o, and p. You want the RHS only to be 
evaluated when the pattern matches, so you will need RuleDelayed (:>) 
and all is good, e.g. for example 2:

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

hth,

albert

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Difficulty with patterns "McHale, Paul" <Paul.McHale@excelitas.com> - 2011-10-23 10:30 +0000
  Re: Difficulty with patterns Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-10-24 09:18 +0000
  Re: Difficulty with patterns A Retey <awnl@gmx-topmail.de> - 2011-10-25 10:16 +0000

csiph-web