On Jan 16, 11:37=A0pm, RichardOnRails
<RichardDummyMailbox58... / USComputerGurus.com> wrote:
> On Jan 16, 7:06=A0pm, Josh Cheek <josh.ch... / gmail.com> wrote:
>
>
>
> > [Note: =A0parts of this message were removed to make it a legal post.]
>
> > On Sun, Jan 16, 2011 at 5:45 PM, RichardOnRails <
>
> > RichardDummyMailbox58... / uscomputergurus.com> wrote:
> > > Hi,
>
> > > I've got a text file with 7 fields. =A0I successfully extract the dat=
a
> > > on each with a regex and for debugging purposes print each lines
> > > fields.
>
> > > Now I want to edit and organize the data for subsequent processing.
> > > But populating the hash fails. I posted the code on
> > >http://www.pastie.org/1468271
>
> > > The "puts" on line 15 shows that the regex captures the 7 fields in
> > > the text file.
> > > Lines 17-19 is an attempt to build a hash using the known values in
> > > the $n variables.
> > > That attempt fails because line 21 shows that it's nil instead the
> > > string 100.0000 reported by line 15.
>
> > > Maybe I should just build an array of the extracted values and use th=
e
> > > symbols I've created to index the array. =A0In the mean time, =A0main=
ly
> > > for my education, I'd like to know where I went wrong with this hash.
>
> > > Thanks in Advance,
> > > Richard
>
> > Hi, Richard. It looks like your problem is that you put your data into =
your
> > hash with `:n_shares=3D>$2`, on line 17, but you pull it out with `puts
> > h[:shares]`, on line 21. When you get in weird situations like that, wh=
ere
> > it seems correct on one line, and incorrect just a few later, give your=
self
> > a sanity check by breaking it down into as small of steps as possible. =
I'd
> > think "well, if it isn't in there, then wtf does my hash look like?" an=
d
> > instead of `puts h[:shares]` I'd stick a `p h` in there, which will pri=
nt
> > out an inspected version of the array. At that point, I'd see the data
> > exists, and know the problem must be how I'm pulling it out.
>
> > Also, I don't think `h.each_pair { |n,v| h[n] =3D v }` does anything, y=
ou are
> > asking it for each key and value that it has, then telling it to set th=
at
> > key to that value. Well, if it gave you that pair, then it is already s=
et.
>
> Hi Josh,
>
> Thanks for your reply. =A0I wrote that stupid stuff late last night and
> didn't like what I was composing. =A0I wrote one approach for
> initializing a hash and liked the result well enough, but didn't like
> the copy & paste I did to create it. =A0(I do as much cut and paste as I
> can 'cause I hate to feel like a clerk-typist.)
>
> So I had two redundant thing coded. =A0And when I looked at it today, =A0=
I
> forgot what I was doing, so I sent out my SOS.
>
> > give yourself a sanity check by breaking it down into as small of steps=
 as possible.
>
> That's in keeping with Agile Programming's principle, =A0and I'm
> striving to embrace that. =A0I'm gearing up to actually write my RSpec
> stuff first before coding functionality.
>
> > I'd think "well, if it isn't in there, then wtf does my hash look like?=
" and instead of
> > `puts h[:shares]` I'd stick a `p h` in there.
>
> That's a great point. =A0I should wean myself off writing so much code
> to inspect my code's result and use Ruby's inspect in the form of the
> 'p' command. =A0Lesson learned, I hope.
>
> This is my current idea for succinct code to support further data
> analysis:
>
> =A0 =A0 =A0 =A0 field_names =3D%w<Name Shares Opened_MDY Closed_MDY Proce=
eds Cost
> GainLoss>
> =A0 =A0 =A0 =A0 h =3D {}
> =A0 =A0 =A0 =A0 (1..field_names.size).each { |name| h[name] =3D eval($ + =
i.to_s) }
>
> Of course, that doesn't work, =A0but despite my perusing of Pragmatic's
> Programming Ruby, 2nd ed., =A0I haven't so far figured out how to code
> that third line. =A0Any ideas. =A0If you're too busy, or whatever, =A0don=
't
> bother responding and I'll post about it tomorrow.
>
> Many thanks for your thoughtful help.
>
> Best wishes,
> Richard

Hey Josh,

Please forget about this last question.  It is garbled.  I'm posting
it anew.

Thanks,
Richard