Dynamically caputure values which is sperated by ','

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 …

line = “properties_count,Type,Total,Voice,Data”

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

–output:–
properties_count
Type
Total
Voice
Data

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 .

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

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

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:

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

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]

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