------extPart_000_006B_01C7FDDC.07D99180
Content-Type: text/plain;
	charsets-ascii"
Content-Transfer-Encoding: 7bit

> -----Original Message-----
> From: Sebastian Hungerecker [mailto:sepp2k / googlemail.com] 
> Sent: Sunday, September 23, 2007 11:43 AM
> To: ruby-talk ML
> Subject: Re: Split into hash
> 
> Lee Jarvis wrote:
> > Ok guys, I have a file which looks something like this:
> >
> > one> > two > > threemething
> > fourmething else
> >
> > I want to read each line into a hash, key
lue..
> 
> Hash[*File.read(filename).scan(/^.*$/).flatten]
> 

I believe that should be:

Hash[*File.read(filename).scan(/^(.*)*)$/).flatten]



Also, the larger the file is, the better the OP's solution (slightly changed
by a chomp to get rid of newlines):

a  ash.new
File.open('file').each do |line|
  b,c  ine.chomp.split(/
  a[b]  
End

will scale. Here's a test with 100_000 key/value pairs:

$ for a in `seq 1 100000`; do echo "$a
" >> keyval; done
$ irb
irb(main):001:0> require 'benchmark'
true
irb(main):002:0> Benchmark.bm do |x|
irb(main):003:1* x.report {
irb(main):004:2* a  ash.new
irb(main):005:2> File.open('keyval').each do |line|
irb(main):006:3* b,c  ine.chomp.split(/
irb(main):007:3> a[b]  
irb(main):008:3> end
irb(main):009:2> }
irb(main):010:1> x.report {
irb(main):011:2* Hash[*File.read('keyval').scan(/^(.*)*)$/).flatten]
irb(main):012:2> }
irb(main):013:1> end
      user     system      total        real
  0.640000   0.020000   0.660000 (  0.665282)
  5.410000   0.030000   5.440000 (  5.433251)
true
irb(main):014:0> 

Though for 10_000 pairs both versions run at about the same speed on my
system, and for 1_000 the regex scanner is slightly faster. So I suppose
it'll depend on the use case.

> 
> HTH,
> Sebastian
> -- 
> NP: Black Sabbath - Dirty Women
> Jabber: sepp2k / jabber.org
> ICQ: 205544826

Felix 

------extPart_000_006B_01C7FDDC.07D99180
Content-Type: application/x-pkcs7-signature;
	namemime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
	filenamemime.p7s"

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUr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