0

I've read an input file and extracted each column in to individual arrays like below.

id = ["id\a_po87y", "id\ruio66", "id\rzd766", "id\ruio66", etc..]

store = ["Jack", "John_Marsha", "123_Smart_option", "John_Marsha", etc...]

group = ["leader", "leader_1", "fresher", "automation_dev", etc...]

id_details.txt (input file)

id\a_po87y  Jack    leader
id\ruio66   John_Marsha leader_1
id\rzd766   123_Smart_option    fresher
id\ruio66   John_Marsha automation_dev
....
etc

How should I iterate the arrays in a nested fashion in such a way that, the first element of the array - 'id'(id\a_po87y) should be checked for its preence in the file Jack_details.txt (1st element of the store array) and should be deleted from the row name - leader (again 1st array element of group array)

Likewise, id\ruio66 should be checked for its availabilty in the 2dn file - John_Marsha in the row id - leader_1 and if present, remove it.

so on..

id[0]--> store[0]-->group[0]

id[1]--> store[1]-->group[1]

and so on

My code, but the values are skipped in the array elements

file_dir = 'E:/ruby_work'

file = File.open("E:/ruby_work/id_details.txt", "r")
contents = file.each_line.map { |line| line.split("\t") }.transpose
id, file_name, group  = contents

id.each do |ids|
  puts "For ID: #{ids}"
  file_name.each do |name| 
    value = File.open("#{file_dir}/#{name}_details.txt")
    text = File.read(value)
    #puts text
    text.each_line do |el|
      group.each do |gr|
        if el.match(/#{gr}/) then 
          print "group row #{gr}\n"
          print "Its matching\n"
          replace = text.gsub( /#{Regexp.escape(ids)}\,\s/, '').gsub( /#{Regexp.escape(ids)}/, '' ).gsub /,\s*$/, ''
        else print "Not\n"
          print "group row #{gr}\n"
        end
        group.shift
      end 
    end
    file_name.shift
  end
end  
id.shift

What i'm doing wrong?

Jack_details.txt

Joined on Feb 7, 2016
Created by: Solomon (ruio66) 

[groups]
leader_1 = id\rty67, id\mztrt, id\ruio66, ncr\025kc, id\a_po87y
automation = id\bzo0l4, ccr\poxz7j
automation_dev = id\ruio66

John_Marsha_details.txt

Joined on Jan 7, 2016
Created by: Jack Rondon 

[groups]
leader_1 = id\sop0r2, id\34_dev, id\mz4d5, id\ruio66
fresher = id\kzpo98, id\gz8sl7, id\cp0jxr, id\fzxlol, 
automation_dev = id\ruio66

Output

For ID: id\a_po87y
Not
group row leader

Not
group row leader_1

For ID: id\ruio66
3
  • You are modifying the array while you are iterating over it. That cannot possibly work. Commented May 27, 2017 at 13:16
  • @Goku I'd like to help you but cannot understand what you really want. Could you please provide sample input files with corresponding outputs? Then mention me in commentary and I'll help you. Commented May 27, 2017 at 16:34
  • @DjezzzL: My apologies for delayed reply. I have edited the question with the sample input files and the output . Thanks much for your time. Commented May 29, 2017 at 11:03

1 Answer 1

1

If i understood correctly, maybe you could take a slightly different approach:

Considering this

id[0]--> store[0]-->group[0]
id[1]--> store[1]-->group[1]

then you don't need to nest loops, you could try looping only once and use an index to fetch each value form each array, something like this:

(0..id.size - 1).each do |i|
  puts "For ID: #{id[i]}"
  value = File.open("#{file_dir}/#{file_name[i]}_details.txt")
  text = File.read(value)

  text.each_line do |line|
    if line.match(/#{group[i]}/)
      print "group row #{group[i]}\n"
      print "Its matching\n"
      replace = text.gsub(/#{Regexp.escape(id[i])}\,\s/, '')
                    .gsub(/#{Regexp.escape(id[i])}/, '')
                    .gsub(/,\s*$/, '')
    else
      print "Not\n"
      print "group row #{group[i]}\n"
    end
  end
end

This code assumes that id, file_name and group will always have the same size.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a ton. Yes the arrays are of same size. Sorry for the delayed reply, im curious whats the mistake I've done.
@Goku Your mistake was that you modified your array while being iterated (with .shift); that is, you removed the first element of the array on each iteration, making your array smaller each time but the internal index (used to loop the array) still incremented by one. For example, consider an array of ["a", "b", "c"]. In first loop index will be 0, and that corresponds to the first element, that is "a", now you remove "a" from so your array becomes ["b", "c"]. On second loop index is 1, and that corresponds to "c". So you skipped "b".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.