[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Split into hash

Lee Jarvis

9/23/2007 6:23:00 PM

Ok guys, I have a file which looks something like this:

one=1
two=2
three=something
four=something else

I want to read each line into a hash, key=value..

I have tried something like

File.open('file').each do |line|
line.split(/=/){|a,b| #something}

but that's kind of how far i get.. The only way i know of is reading
them all into an array of arrays and turning that into a hash, Is there
a better idea?

Thanks in advance
--
Posted via http://www.ruby-....

3 Answers

Lee Jarvis

9/23/2007 6:30:00 PM

0

Ok now i have

a = Hash.new
File.open('file').each do |line|
b,c = line.split(/=/)
a[b] = c
end


Which works, but im sure there are better ways out there?
--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/23/2007 6:43:00 PM

0

Lee Jarvis wrote:
> Ok guys, I have a file which looks something like this:
>
> one=1
> two=2
> three=something
> four=something else
>
> I want to read each line into a hash, key=value..

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


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

Ian J. Ball

5/8/2013 4:59:00 AM

0

On May 7, 9:12 pm, "Adam H. Kerman" <a...@chinet.com> wrote:
> Ian J. Ball <ijb...@mac.com> wrote:
>
> >>http://www.hollywoodreporter.com/live-feed/tv-pilots-series......
> >>10 TV Pilots Likely to Score Series Pickups
> >>by Lesley Goldberg
> >>[snip]
> >>10. Reign (CW)
> >>The CW will likely hand out three additional series orders beyond
> >>Plec's Vampire Diaries spinoff The Originals and add the drama about
> >>Mary Queen of Scots' rise to power as its big swing. Sources say the
> >>network likes the international appeal of Reign, which filmed in
> >>Europe and counts a largely unknown cast led by Adelaide Kane in her
> >>second stateside role (she'll next be seen in the upcoming third
> >>season of MTV's Teen Wolf.)
>
> >No shit! CWN actually picks this up?! Over their other obvious
> >dreck??!!
> >Maybe that netlet ain't dead yet...
>
> I thought it was already announced. Didn't they make 'em add extra
> teenage angst strictly for the American audience that won't be seen over there?

Neither thing has been announced, AFAIK...