Loop through array and create new field values

I have an xml filter creating two arrays from an xml sql column, there could be any amount of values in the arrays.

xml {       
    source => "event_xml"              
    remove_namespaces => true 
    store_xml => false
    xpath => [ "/Data/Name/text()", "Key" ,
               "/Data/Value/text()", "Value" ]
  }

I need to loop through each array to make sure its not null and has same count as the 2nd array.

Then create new fields and the values from said arrays. my ruby code below is;

ruby {
    code => '
      keys = event.get("Key")
      values = event.get("Value")      
      if keys.length == values.length
        keys.each_index { |i| event.set("keys[i]", values[i]) }
      end
    '
  }

ruby exception being received.

invalid FieldReference: keys[i]`

Is what i'm trying to do possible or is it ruby syntax off?

thanks

ruby {
code => '
keys = event.get("[Key]")
values = event.get("[Value]")
if not(keys == nil) && (keys.length == values.length)
keys.each_with_index { |val, i| event.set("#{val}", values[i]) }
end
'
}