Consider the variables:
ctr = ['cobol',nil,nil,'test',nil,'cobol', nil]
h1 = {
0=>{"ABC"=>"10000100126N", "CDE"=>"2013-08-30-}", "TPP"=>"11400000206633458812N", "APD"=> "01531915972", "PRODUCTID"=>"113n", "OPP"=>"201509n", "CTC"=>"C"},
1=>{"ABC"=>"00000039540A", "CDE"=>"0182.22X", "TPP"=>"1234.565N", "APD"=>"12345600", "PRODUCTID"=>"ACHN", "OPP"=>"00000000000119964.1256", "CTC"=>"00000000000211920"}
}
h2 = {'{' => '+0', 'A' => '+1', 'B' => '+2', '}' => '-0', 'N' => '-5'}
The task is to read the ctr
data and where the value is cobol
, we need to apply logic for those values in h1 hash only.
we need to parse the hash h1
and if the last char in hash's value matches with one of the key in hash h2
then replace that value with the corresponding value and prepend symbol to the string.
For example: when we scan hash h1
, for value "10000100126N", as the last char is N and it exists in h2
, then the output should be '-100001001265' where 5 is appended and - is prepended. [Not that the ctr for this is 'cobol']
But if we look at the second value "CDE"=>"2013-08-30-}", since for this key-value pair, the ctr value is not cobol, wee do nothing with the string.
This is what i have done so far:
h1.each do |k,h|
h.update(h) do |*, v|
# puts v
h2.each do |q,p|
if (v[-1] == q)
v.sub!(v[-1], p[-1])
v.sub!(/(.*?)/, p[0] +'\1')
end
end
v
end
end
This code is updating the string as per the requirement, but its running for all the values in h1
, i need to run the code only for the corresponding index where the value in the array ctr
is 'cobol'