From: RichardOnRails Date: 2011-01-17T23:45:38+09:00 Subject: Re: A tad of meta-programming needed On Jan 17, 1:05 am, Josh Cheek wrote: > [Note:  parts of this message were removed to make it a legal post.] > > On Sun, Jan 16, 2011 at 11:30 PM, RichardOnRails < > > > > RichardDummyMailbox58...@uscomputergurus.com> wrote: > > I've got the following hash set up and it works fine.  But this took > > more typing than programming. > > >        h = {:name=>$1,  :n_shares=>$2,  :opened_MDY=>$3,  :closed_MDY=>$4, > >                :proceeds=>$5,  :cost=> $6,   :gain_Loss=>$7 > >              } > > > So I'd like to get the following to work: > > >        field_names =%w > GainLoss> > >        h = {} > >        (1..field_names.size).each { |i| h[field_names[i]] = eval($ + > > i.to_s) } > > > But concatenating the character "$" with the value of i converted to a > > string, followed be evaluating that as a local variable isn't working > > as I coded it.  Can it be easily corrected? > > > Thanks in Advance, > > Richard > > The matchdata object implements the same inteface as a hash. If that is all > you need, on 1.9 you could just name the capture groups, then use the > MatchData object and not have to do any assignment/creation at all. > (Assuming this data is based on the pastie you posted earlier, this code > would work) > > RUBY_VERSION # => "1.9.2" > > line = "Apple 1.1234 1/2/3 4/5/6 $1,234.56 $7 ($5)" > > regex = /^(?       .+                       )\s >           (?   \d+\.\d{4}               )\s+ >           (? \d\d?\/  \d\d?\/  \d\d?  )\s+ >           (? \d\d?\/  \d\d?\/  \d\d?  )\s+ >           (?   [\$\d,\.]+               )\s+ >           (?       [\$\d,\.]+               )\s+ >           (?  [\(\)\$,\d \.]+          )\s* >         $/x > > result = regex.match(line) > result                      # => # $1,234.56 $7 ($5)" name:"Apple" n_shares:"1.1234" opened_MDY:"1/2/3" > closed_MDY:"4/5/6" proceeds:"$1,234.56" cost:"$7" gain_Loss:"($5)"> > result[ :name       ]       # => "Apple" > result[ :n_shares   ]       # => "1.1234" > result[ :opened_MDY ]       # => "1/2/3" > result[ :closed_MDY ]       # => "4/5/6" > result[ :proceeds   ]       # => "$1,234.56" > result[ :cost       ]       # => "$7" > result[ :gain_Loss  ]       # => "($5)" Hi Josh, Wow! I posted this question ~ midnight and shutdown for the night, except for re-reading the Reflections chapter in Pragmatic's "Programming Ruby". 2nd ed. Got up at 9, expecting to tackle this problem again, but first opened this thread hopefully, and was rewarded with a tutorial on part of MatchData applicable to my goal. That's too good to be true. > The matchdata object implements the same inteface as a hash. If that is all > you need ... That is precisely all I need. > on 1.9 ... I've been running 1.8.6 for a good while. I've been tempted to advance to to 1.9.x in get Look Ahead or Look Behind in regex's, whichever in missing in 1.8.6. But now I've got a definite purpose to upgrade! Thank you very much for your excellent solution. I'm burdened this problem because the IRS wants tax reports for past years when I lost a little money as a day trader in the stock market. You might be surprised at how many trades you can amass in a year if you're at it almost every weekday each year. The IRS has instituted a new rule which took effect this year: brokerages must report not merely proceeds from sales, but also the related cost and hence the gain or loss. Duh! Hopefully, the new rule will preclude the recurrence of this issue. Best wishes, Richard An innocent citizen :-) I won't need to beg for any more help for a good while, I hope. Best wish