ruby - Comparing each line in CSV file to JSON hash on matching element to get unique matches only -


i searched before posting perhaps don't know correct search terms.

i read csv file new array , trying compare eighth element in array json response of program. json response program (data["ip"]) ip address matches in row[8].

everything working matches, 3 non-matches found in every loop iteration. ip address doesn't exist in json 100.100.100.300, others have match.

my code:

require 'csv' require 'json' require 'date'  csv.foreach("list.csv", :headers => true) |row|   @response["response"]["results"].each |data|       if row[8].eql?(data["ip"])       puts "match: " + row[8] + " = " + data["id"]     else       puts "no match found " + row[8]     end   end end 

here results like:

match: 100.100.100.200 = 26826 match: 100.100.100.100 = 26827 match: 100.100.100.400 = 26827 no match found 100.100.100.300 

here results getting:

no match found 100.100.100.200 match: 100.100.100.200 = 26826 no match found 100.100.100.200 no match found 100.100.100.200 no match found 100.100.100.300 no match found 100.100.100.300 no match found 100.100.100.300 no match found 100.100.100.300 match: 100.100.100.100 = 26827 no match found 100.100.100.100 no match found 100.100.100.100 no match found 100.100.100.100 no match found 100.100.100.400 no match found 100.100.100.400 no match found 100.100.100.400 match: 100.100.100.400 = 26827 

'list.csv' looks this:

applicability,person,tier,type,atype,name,code,sub,ip address,location,presence,env,main,main2,status,contact,group,fqdn,outside,outsideident,category,comments yes,unit,accepted,level,hardware,namea,c1234565,yes,100.100.100.200,"nowhere",no,vp,running,somekind,active,"","",fullname,"","",tool,"" yes,unit,accepted,level,hardware,nameb,c1234566,yes,100.100.100.300,"nowhere",no,vp,running,somekind,inactive,"","",fullname,"","",tool,"" yes,"","",something,hardware,namec,c1234567,yes,100.100.100.100,"nowhere",,"",,,"","name, contact","",fullnamefull,"","",tool,"" yes,"","",something,hardware,named,c1234568,no,100.100.100.400,"nowhere",,"",,,"","name, contact","",fullnamefull,"","",tool,"" 

you may not need 2 loops. below: (not tested json structure not shared in question)

results = @response["response"]["results"]  csv.foreach("list.csv", :headers => true) |row|   matching_entry  = results.find { |data| row["ip address"] == data["ip"]}   if matching_entry     puts "match: " + row["ip address"] + " = " + matching_entry["id"]   else     puts "no match found " + row["ip address"]]   end end 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -