Forum: Ruby Dynamically caputure values which is sperated by ','

7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-07-24 15:36
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 ..
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-07-24 20:23
line = "properties_count,Type,Total,Voice,Data"

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

--output:--
properties_count
Type
Total
Voice
Data
7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-07-25 09:54
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 .
7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-07-25 10:19
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
7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-07-25 10:34
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
7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-07-25 11:52
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-07-26 21:28
Fosiul Alam 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:

http://www.ruby-doc.org/core-2.1.2/Array.html
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-07-26 21:38
Fosiul Alam 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]
5f57871af762854c5d9883ffd393c38d?d=identicon&s=25 Anand Ramanathan (rcanand)
on 2014-07-29 16:51
7stud -- wrote in post #1153611:
> Fosiul Alam 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.