0

I need to generate an array checking some conditions. Here is the scenario.

I have 3 variables containing strings like this:

client1 = "Google"
client2 = "Apple"
client3 = "Microsoft"

category1 = "sales"
category2 = "software"
category3 = "hardware"

The output i need to generate is an array which should have all the clients and the categories appended together by an underscore "_".

Desired Output: array = ["Google_sales", "Apple_software", "Microsoft_hardware"]

What I have tried so far:

array = [client1+"_"+category1, client2+"_"+category2, client3+"_"+category3]

Now, this works fine and I get what I want. But the complexity starts when a variable is empty. Consider there is another variable called client4="" and category4="". Now these are empty and when i try to complete my array i get messy array values.

Ex: array = [client1+"_"+category4, client4+"_"+category2]

This would give me an output like this: array = ["Google_", "_software"]

Question:

The user fills in the clients and the category of the clients. There might be a chance where the user failed to enter client or a category. Currently I have client1,client2,client3,client4 and cat1,cat2,cat3,cat4. Client1 is associated with cat1 and so on. Now I need to get an array only with the valid entries and if one of them is empty then we skip to the next one.

So we insert "_" in between client1 and cat1 only if both are present. Else we move to client2 and cat2 and so on.

7
  • Quick question, wouldn't it make more sense to store an array of hashes like so: [{client => category}, {client => category}] etc? On a secondary note, how do the clients and categories come into this method? How do you iterate over them to construct the array? How do you know how many are present? Commented Feb 21, 2013 at 20:04
  • I tried it, but felt crating it as an array would be more easier as I want the final output to be as a string so finally i do array.join(","). If you have better views, please post it as an answer. Thanks Commented Feb 21, 2013 at 20:06
  • Can you please elaborate on my other questions as well so I can craft a more tailored answer? Thanks! Commented Feb 21, 2013 at 20:06
  • The user fills in the clients and the category of the clients. There might be a chance where the user failed to enter client or a category. Currently I have client1,client2,client3,client4 and cat1,cat2,cat3,cat4. Client1 is associated with cat1 and so on. Now I need to get an array only with the valid entries and if one of them is empty then we skip to the next one. Commented Feb 21, 2013 at 20:10
  • So we insert "_" in between client1 and cat1 only if both are present. Else we move to client2 and cat2 and so on. Hope you understood. Thanks Commented Feb 21, 2013 at 20:13

3 Answers 3

2

Use Array#zip and Array#join

The easiest way to do this would be to use Array#zip and Array#join. For example:

companies  = %w[Google Apple Microsoft]
categories = %w[sales software hardware]
companies.zip(categories).map { |array_pair| array_pair.join '_' }
# => ["Google_sales", "Apple_software", "Microsoft_hardware"]

You can make this more complicated if you want, but as long as you have the same number of items in each array, this is about as simple as it gets.

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

Comments

1

I'm going to make a few assumptions here:

1) Your method takes as input two arrays: an array of cliens and an array of categories .

2) If the user neglects to enter a value, an empty string is saved in its place.

3) As outlined in your comment, your desired final output is a commaseparated string.

def collect_clients_and_customers(clients, categories)
   returnable = ""
   if clients.size != categories.size
      puts "O MY GOD! SOMETHING HAS GONE TERRIBLY WRONG"
      nil
   else
      clients.each_with_index do |client, index|
          unless client[index].empty? or categories[index].empty?
               returnable = returnable+client[index]+"_"+categories[index]+","
          end
          if index == clients.size-1
               returnable = returnable[0..index-1]
          end
      end
   end
   returnable
 end

Comments

1
array = [
  [client1, category1], [client2, category2], [client3, category3],
  [client1, category4], [client4, category2],
]
.map{|a| a.join("_") unless a.any?(&:empty)}.compact

7 Comments

But what if the length of the list is unknown or arbitrary? Also the last two items in your array appear wrong to me....
For your first point, the OP did not mention that possibility. I can't parse your second sentence.
Second sentence: [client1, category4], [client4, category2] should be [client4, category4]. Also, you have a trailing comma
Why should it be like that? The example is made by combining the two arrays the OP showed to illustrate a general case. And yes, I do have a trailing comma. So what?
If you read the OP's later comments, specifically "The user fills in the clients and the category of the clients. There might be a chance where the user failed to enter client or a category. Currently I have client1,client2,client3,client4 and cat1,cat2,cat3,cat4. Client1 is associated with cat1 and so on. Now I need to get an array only with the valid entries and if one of them is empty then we skip to the next one." I guess we need to wait for the OP to clarify, but my understanding was he wanted to join 1 with 1, 2 with 2, 3 with 3, etc...
|

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.