0

I have an array like this I use

       inputx.scan(/.*?\n/)

for create array this is a representation of my array

      element 1 => [car;dog;soda]
       element 2 => [bunny;pc;laptop]
   element 3  => [hand;sword;shield]

this is my text file I use scan method for create array inputx.scan(/.*?\n/)

     car;dog;soda

       bunny;pc;laptop

     hand;sword;shield

I need to replace each comma by number of array for obtain this

this is my expected output

in this output I replace ";" by "nthelementnumber;" example 1;

      car1;dog1;soda

        bunny2;pc2;laptop

       hand3;sword3;shield

Please help me

1
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a minimal reproducible example. Commented Jun 9, 2017 at 1:21

1 Answer 1

2

It's a bit hard to tell what exactly your array looks like, but I'm going to take a guess:

element = ['car;dog;soda',
           'bunny;pc;laptop',
           'hand;sword;shield']

If that's correct, you can get the output you are looking for with something like:

element.each_index {|i| element[i] = element[i].gsub(';', "#{i+1};")}

The each_index iterator gives you each index (unsurprisingly). Then you can use each index to manipulate each value in the array.

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

1 Comment

yes works very thank you help me to complete my code ; I am very gratefully :)

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.