I have a loop that I am trying to put key values pairs into a hash from. There are 6 rows of data, when I do a outputHash.inspect all I get is the last value. Where do the other values go or what I am I doing wrong? row.each {|line| line =~ /^\s*\d+\s*\S+\s*\S+\s*(\d+)\s*\D+(\d+)/ port_num = $1 if $1 status = $2 =~ /0/ ? "Available" : "Not Available" outputHash = {port_num => status} } outputHash.inspect --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, Jul 29, 2008 at 10:04 PM, Me <chabgood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have a loop that I am trying to put key values pairs into a hash > from. There are 6 rows of data, > when I do a outputHash.inspect all I get is the last value. Where do > the other values go or what I am I doing wrong? > row.each {|line| > line =~ /^\s*\d+\s*\S+\s*\S+\s*(\d+)\s*\D+(\d+)/ > port_num = $1 > if $1 > status = $2 =~ /0/ ? "Available" : "Not Available" > outputHash = {port_num => statusYou are overwriting outputHash here. You probably want outputHash[port_num] = status> } > outputHash.inspect > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 30 Jul 2008, at 04:04, Me wrote:> > I have a loop that I am trying to put key values pairs into a hash > from. There are 6 rows of data, > when I do a outputHash.inspect all I get is the last value. Where do > the other values go or what I am I doing wrong? > row.each {|line| > line =~ /^\s*\d+\s*\S+\s*\S+\s*(\d+)\s*\D+(\d+)/ > port_num = $1 > if $1 > status = $2 =~ /0/ ? "Available" : "Not Available" > outputHash = {port_num => status} > } > outputHash.inspect >outputHash = {port_num => status} This creates a new hash each time. You want outputHash[port_num]=status (and the variable needs to exist before the block (becaused of scoping) Fred> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---