1

I have two arrays

ids = ["10.12.14","10.12.15"]
iq = ["abc","pqr"]

I want o/p in below format

New-IscsiTargetPortal -TargetPortalAddress 10.12.14
New-IscsiTargetPortal -TargetPortalAddress 10.12.15
Connect-IscsiTarget -NodeAddress abc -TargetPortalAddress 10.12.14 -IsPersistent $True
Connect-IscsiTarget -NodeAddress xyz -TargetPortalAddress 10.12.15 -IsPersistent $True

My code looks like

for i in ids
  for j in iq
    puts "New-IscsiTargetPortal -TargetPortalAddress #{i}"
    puts "Connect-IscsiTarget -NodeAddress #{j} -TargetPortalAddress #{i} -IsPersistent $True"
  end
end

But it is duplicating values for i and j:

New-IscsiTargetPortal -TargetPortalAddress 10.12.14
Connect-IscsiTarget -NodeAddress abc -TargetPortalAddress 10.12.14 -IsPersistent $True
New-IscsiTargetPortal -TargetPortalAddress 10.12.14
Connect-IscsiTarget -NodeAddress pqr -TargetPortalAddress 10.12.14 -IsPersistent $True
New-IscsiTargetPortal -TargetPortalAddress 10.12.15
Connect-IscsiTarget -NodeAddress abc -TargetPortalAddress 10.12.15 -IsPersistent $True
New-IscsiTargetPortal -TargetPortalAddress 10.12.15
Connect-IscsiTarget -NodeAddress pqr -TargetPortalAddress 10.12.15 -IsPersistent $True

Can you please help me in nested for loop or how i can get desired o/p?

1
  • I believe "xyz" should be "pqr". Commented Dec 19, 2017 at 17:07

4 Answers 4

2

For the first part, you can just iterate ids:

ids.each do |i|
  puts "New-IscsiTargetPortal -TargetPortalAddress #{i}"
end

For the second part, you can combine ids and iq pair-wise using Array#zip:

ids.zip(iq) do |i, j|
  puts "Connect-IscsiTarget -NodeAddress #{j} -TargetPortalAddress #{i} -IsPersistent $True"
end

Output:

New-IscsiTargetPortal -TargetPortalAddress 10.12.14
New-IscsiTargetPortal -TargetPortalAddress 10.12.15
Connect-IscsiTarget -NodeAddress abc -TargetPortalAddress 10.12.14 -IsPersistent $True
Connect-IscsiTarget -NodeAddress pqr -TargetPortalAddress 10.12.15 -IsPersistent $True
Sign up to request clarification or add additional context in comments.

Comments

1
▶ result = ids.zip(iq).
▷   flat_map do |ip, name|
▷     ["New #{ip}", "Connect #{name} :: #{ip}"]
▷   end.partition { |s| s.start_with? 'New' }.flatten
#⇒ [
#  [0] "New 10.12.14",
#  [1] "New 10.12.15",
#  [2] "Connect abc :: 10.12.14",
#  [3] "Connect pqr :: 10.12.15"
# ]

▶ result.each(&method(:puts))
New 10.12.14
New 10.12.15
Connect abc :: 10.12.14
Connect pqr :: 10.12.15

Comments

0

Use each_with_index and use the index of the first array to determine which element to retrieve on the second array.

ids.each_with_index do |id, index|
    puts "New-IscsiTargetPortal -TargetPortalAddress #{id}"
    puts "Connect-IscsiTarget -NodeAddress #{iq[index]} -TargetPortalAddress #{id} -IsPersistent $True"
  end
end

Comments

0

If you can rely on the two kinds of data being connected (ie 10.12.14 > "abc"), then you could structure the data better before processing to make this easier:

h = [{
      target: "10.12.14",
      node: "abc"
    },
    {
      target: "10.12.15",
      node: "xyz"
    }]

Following this, you'll need to loop once exclusively for the target addresses (array a) to get the first two lines, and then a second time for the combined information lines, which would look something like this:

for o in h
  puts "New-IscsiTargetPortal -TargetPortalAddress #{o[:target]}"     
end

for o in h
  puts "Connect-IscsiTarget -NodeAddress #{o[:node]} -TargetPortalAddress #{i[:target]} -IsPersistent $True"
end

Comments

Your Answer

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