# Dynamically caputure values which is sperated by ','

**URL:** <https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166>\
**Category:** Ruby\
**Created:** [July 24, 2014, 3:36pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166 "2014-07-24T15:36:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [July 24, 2014, 3:36pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/1 "2014-07-24T15:36:44Z")

</div>

cat properties.txt

properties\_count,Type,Total,Voice,Data  
Traffic\_count,DR\_TYPE,CREATED\_FILES,CREATED\_FILES,BULKED\_FILES

what i am trying to do is,

file\_properties=File.open(‘properties.txt’)  
file\_properties.each do |line|

```
     cn = line.split(',')
    header_name = cn[0]
    puts "lie header will be #{header_name}"
    puts "Bellow is rest of the values other then #{header_name}"

    QUESTOIN: HOW DO I SHOW REST OF VALUES HERE BUT NOT CN[0]

```

end

So when i will run it , it will show liek bellow

line header will be CDR\_count  
Bellow is rest of the values other then “CDR\_count”  
Type  
Total  
Voice  
Data  
lie header will be xDR\_Traffic  
Bellow is rest of the values other then xDR\_Traffic  
DR\_TYPE  
CREATED\_FILES  
CREATED\_FILES  
BULKED\_FILES

Please let me know if the question is confusing …

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 24, 2014, 8:23pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/2 "2014-07-24T20:23:38Z")

</div>

line = “properties\_count,Type,Total,Voice,Data”

pieces = line.split(’,’)  
puts pieces[0]  
puts pieces[1…-1]

–output:–  
properties\_count  
Type  
Total  
Voice  
Data

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 25, 2014, 9:54am UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/3 "2014-07-25T09:54:55Z")

</div>

Hi  
thanks for the response, but there is an issues when i am pusing values  
into array and when i am trying to retrieve from array

line = “properties\_count,Type,Total,Voice,Data”

pieces = line.split(’,’)  
puts pieces[0]  
puts pieces[1…-1] : This is fine,

table\_td\_header.push(pieces[1…-1]) : WHEN I AM PUTTING VALUES INTO AN  
ARRAY

table\_td\_header.each do |header\_key|

```
     puts "myheader #{header_key}"

 end

```

IT COMMING LIEK BELLOW all in one word , it does not new line:-

myheader Tproperties\_countTypeTotalVoiceData

i think its because , when i am doing in array if i do

p table\_td\_header, I GET Bellow

[[“properties\_count”, “Type”, “Total”, “Voice”,“Data \n”]]

but i need to separate those values while pushing into array and  
retrieve  
from array.

Please help me .

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 25, 2014, 10:19am UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/4 "2014-07-25T10:19:34Z")

</div>

here is the full code

line = “properties\_count,Type,Total,Voice,Data”

myarray=Array.new  
pieces = line.split(’,’)  
puts pieces[0]  
myarray.push(pieces[1…-1])

myarray.each do |key|

```
    puts "mykey:-#{key}"

```

end

output :-  
properties\_count  
mykey:-TypeTotalVoiceData

but i wanted out put of my array to be like:

mykey:-Type  
mykey:-Total  
mykey:-Voice  
mykey:-Data

how do i do this ?

thanks for your time

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 25, 2014, 11:52am UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/5 "2014-07-25T11:52:26Z")

</div>

i found a solution

array = [‘1’, ‘a’, ‘2’, ‘b’, ‘3’, ‘c’]  
(0…array.length-1).step(2) do |i|  
puts “Letter #{array[i]} is #{array[i+1]}”  
end

let me try with this, let see if it solved my problem

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 26, 2014, 9:28pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/6 "2014-07-26T21:28:42Z")

</div>

Fosiul A. wrote in post #1153457:

> but i need to separate those values while pushing into array and  
> retrieve  
> from array.
> 
> Please help me .

array[range] returns an array, and if you push an array into another  
array, you get this:

x = [10, 20, 30]  
to\_add = [1, 2, 3]

x \<\< to\_add #The \<\< method is a synonym for push()  
p x

–output:–  
[10, 20, 30, [1, 2, 3]]

It sounds like you want to call concat() instead of push():

x = [10, 20, 30]  
to\_add = [1, 2, 3]

x.concat to\_add  
p x

–output:–  
[10, 20, 30, 1, 2, 3]

If you don’t want to change the original array, you can create a new  
array using the + method:

x = [10, 20, 30]  
to\_add = [1, 2, 3]

results = x + to\_add  
p x  
p results

–output:–  
[10, 20, 30]  
[10, 20, 30, 1, 2, 3]

If an Array method doesn’t do what you want, you should peruse the  
Array docs to see if there is another suitable method you can use:

> **[Class: Array (Ruby 2.1.2)](http://www.ruby-doc.org/core-2.1.2/Array.html)**
>
> Class : Array - Ruby 2.1.2

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 25, 2014, 10:34am UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/7 "2014-07-25T10:34:17Z")

</div>

Another way i can do but still its not full fill my purpose

line = “properties\_count,Type,Total,Voice,Data”  
pieces=Array.new  
pieces = line.split(’,’)  
pieces.each do |key|  
puts “mykey:-#{key}”  
end

output :-  
[“properties\_count”, “Type”, “Total”, “Voice”, “Data”]  
mykey:-properties\_count  
mykey:-Type  
mykey:-Total  
mykey:-Voice  
mykey:-Data

but what i want is :-

1)properties\_count will in a seprate value example :  
table\_header=“properties\_count”

and rest “type” to “Data” will be an array, so that when i do this

myarray.each do |key|

```
    puts "mykey:-#{key}"

```

end

I get bellow output:-  
mykey:-Type  
mykey:-Total  
mykey:-Voice  
mykey:-Data

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 26, 2014, 9:38pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/8 "2014-07-26T21:38:36Z")

</div>

Fosiul A. wrote in post #1153460:

> but what i want is :-
> 
> 1)properties\_count will in a seprate value example :  
> table\_header=“properties\_count”
> 
> and rest “type” to “Data” will be an array, so that when i do this
> 
> myarray.each do |key|
> 
> ```
> puts "mykey:-#{key}"
> 
> ```
> 
> end
> 
> I get bellow output:-  
> mykey:-Type  
> mykey:-Total  
> mykey:-Voice  
> mykey:-Data

line = “properties\_count,Type,Total,Voice,Data”  
table\_header, \*keys = line.split(‘,’)

keys.each do |key|  
puts “mykey:-#{key}”  
end

puts table\_header

–output:–  
mykey:-Type  
mykey:-Total  
mykey:-Voice  
mykey:-Data  
properties\_count

Here is how that \* works:

x, y = [1, 2, 3]  
p x  
p y

–output:–  
1  
2

x, \*y = [1, 2, 3]  
p x  
p y

–output:–  
1  
[2, 3]

---

<div class="post-metadata">

**Author:** ![fosiul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ac91a4/32.png) [@fosiul](https://www.ruby-forum.com/u/fosiul)\
**Post date:** [July 29, 2014, 4:51pm UTC](https://www.ruby-forum.com/t/dynamically-caputure-values-which-is-sperated-by/231166/9 "2014-07-29T16:51:17Z")

</div>

7stud – wrote in post #1153611:

> Fosiul A. wrote in post #1153460:
> 
> > but what i want is :-
> > 
> > 1)properties\_count will in a seprate value example :  
> > table\_header=“properties\_count”
> > 
> > and rest “type” to “Data” will be an array, so that when i do this
> > 
> > myarray.each do |key|
> > 
> > ```
> > puts "mykey:-#{key}"
> > 
> > ```
> > 
> > end
> > 
> > I get bellow output:-  
> > mykey:-Type  
> > mykey:-Total  
> > mykey:-Voice  
> > mykey:-Data
> 
> line = “properties\_count,Type,Total,Voice,Data”  
> table\_header, \*keys = line.split(‘,’)
> 
> keys.each do |key|  
> puts “mykey:-#{key}”  
> end
> 
> puts table\_header
> 
> –output:–  
> mykey:-Type  
> mykey:-Total  
> mykey:-Voice  
> mykey:-Data  
> properties\_count
> 
> Here is how that \* works:
> 
> x, y = [1, 2, 3]  
> p x  
> p y
> 
> –output:–  
> 1  
> 2
> 
> x, \*y = [1, 2, 3]  
> p x  
> p y
> 
> –output:–  
> 1  
> [2, 3]

Would this work?

line = “properties\_count,Type,Total,Voice,Data”

myarray=Array.new  
pieces = line.split(‘,’)  
puts pieces[0]

# only change is below

myarray = pieces[1..-1].dup

myarray.each do |key|  
puts “mykey:-#{key}”  
end

# output

properties\_count  
mykey:-Type  
mykey:-Total  
mykey:-Voice  
mykey:-Data
