Re: Lists and Loops
- To: mathgroup at smc.vnet.net
- Subject: [mg113025] Re: Lists and Loops
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Mon, 11 Oct 2010 05:17:45 -0400 (EDT)
- References: <i8s5e7$935$1@smc.vnet.net>
Am 10.10.2010 12:44, schrieb Michaell Taylor:
> I am new to Mathematica and am probably too stuck in other languages, but
> but having a hard time with a simple listing building looping structure.
> The code below is supposed to cycle through a list of stock tickers. For
> each ticker it builds a list of certain financial data elements and adds
> each element to a list. When the list is complete (all financial elements
> obtained), the list is written to a Mysql database. The internal looping
> structure seems to work fine. That is, if I execute the internal loop for a
> single ticker, the results are added to the database as planned.
>
> However, the outer structure causes a problem in that the list continues to
> grow with each ticker. The "mydata={}" line was meant to "reset" the list,
> but doesn't seem to be performing as expected.
>
> tickers = {"VNO", "AMB"}
>
> fprop = {"Average50Day", "Average200Day"}
>
> Do[
> mydata = {t}
> Do[
> mydata = Append[mydata, FinancialData[t, i]],
> {i, fprop}]
> Print[mydata]
> SQLInsert[connt, "reit" , {"ticker", "first", "second"}, mydata]
> {t, tickers}]
>
>
> My error, no doubt is obvious to some. Any guidance would be greatly
> appreciated.
I think you are just missing some ; and , at the right places, I have
not tested it but this might work:
Do[
mydata = {t};
Do[mydata = Append[mydata, FinancialData[t, i]], {i, fprop}];
Print[mydata];
SQLInsert[connt, "reit", {"ticker", "first", "second"}, mydata],
{t,tickers}
]
Using Do loops and Append is often neither elegant nor efficient in
Mathematica, something like this would probably be more Mathematica-ish:
mydata = Map[
Function[{t}, Flatten[{t, FinancialData[t, #] & /@ fprop}]],
tickers
];
SQLInsert[connt, "reit", {"ticker", "first", "second"}, mydata]
Depending on what you try to achieve it also might be more efficient to
insert all your data in one go as I did than to use a single SQLInsert
for each ticker.
hth,
albert