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
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
You are overwriting outputHash here.
You probably want outputHash[port_num] = status
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