Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5814 > unrolled thread
| Started by | "McHale, Paul" <Paul.McHale@excelitas.com> |
|---|---|
| First post | 2011-10-23 10:30 +0000 |
| Last post | 2011-10-25 10:16 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
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
| From | "McHale, Paul" <Paul.McHale@excelitas.com> |
|---|---|
| Date | 2011-10-23 10:30 +0000 |
| Subject | Difficulty with patterns |
| Message-ID | <j80qbl$ae1$1@smc.vnet.net> |
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!
Thanks,
Paul
Paul McHale | Electrical Engineer, Energetics Systems | Excelitas Technologies Corp.
=09
Phone: +1 937.865.3004 | Fax: +1 937.865.5170 | Mobile: +1 937.371.2828
1100 Vanguard Blvd, Miamisburg, Ohio 45342-0312 USA
Paul.McHale@Excelitas.com
www.excelitas.com
Please consider the environment before printing this e-mail.
This email message and any attachments are confidential and proprietary to Excelitas Technologies Corp. If you are not the intended recipient of this message, please inform the sender by replying to this email or sending a message to the sender and destroy the message and any attachments.
Thank you
[toc] | [next] | [standalone]
| From | Armand Tamzarian <mike.honeychurch@gmail.com> |
|---|---|
| Date | 2011-10-24 09:18 +0000 |
| Message-ID | <j83ai0$k5s$1@smc.vnet.net> |
| In reply to | #5814 |
On Oct 23, 9:30 pm, "McHale, Paul" <Paul.McH...@excelitas.com> wrote:
> 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!
>
> Thanks,
> Paul
>
> Paul McHale | Electrical Engineer, Energetics Systems | Excelitas Technologies Corp.
> =09
> Phone: +1 937.865.3004 | Fax: +1 937.865.5170 | Mobile: +1 937.371.2828
> 1100 Vanguard Blvd, Miamisburg, Ohio 45342-0312 USA
> Paul.McH...@Excelitas.comwww.excelitas.com
>
> Please consider the environment before printing this e-mail.
> This email message and any attachments are confidential and proprietary to Excelitas Technologies Corp. If you are not the intended recipient of this message, please inform the sender by replying to this email or sending a message to the sender and destroy the message and any attachments.
> Thank you
it is a bit subtle. You need to use RuleDelayed for this operation
instead of Rule.
Mike
[toc] | [prev] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-10-25 10:16 +0000 |
| Message-ID | <j86298$5kf$1@smc.vnet.net> |
| In reply to | #5814 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web