Writing to a file help

J

JeremyWoertink

I am trying to read 1 file then create another file from that 1. My
problem I am having is that my file is not being formated the same way
I am telling it. So for example I have

line_a = %{#{i.to_s.rjust(6, "0")}|#{array[0]}\|#{array[1]} #{array[2]}
\|#{array[3]}\|#{array[4]}|#{array[5]}|#{array[6]} #{array[7]}|
#{array[8]}|\n}

out_file.write(line_a)


the output is

000001|array0array1array2array3array4array5array6
| |||| ||


so basically my pipes and spaces are being appended to the end of my
string after the "\n"


any ideas?


~Jeremy
 
J

JeremyWoertink

ok, so it seems that it is not splitting the line into an array. so
what looks to be my array0-6 is actually just array[0]. So, my next
question is how do I split a line by tabs?

I was using array << line.split("\t"), but that isn't working.

Ideas?

thanks,

~Jeremy
 
J

JeremyWoertink

HAHA, i'm dumb.. sorry, I forgot that using split returns an array, so
I was appending an array to another one. WOW.... time to lay off the
VB for a while and get my head back in some ruby.


OMG.....


~Jeremy

ok, so it seems that it is not splitting the line into an array. so
what looks to be my array0-6 is actually just array[0]. So, my next
question is how do I split a line by tabs?

I was using array << line.split("\t"), but that isn't working.

Ideas?

thanks,

~Jeremy

I am trying to read 1 file then create another file from that 1. My
problem I am having is that my file is not being formated the same way
I am telling it. So for example I have
line_a = %{#{i.to_s.rjust(6, "0")}|#{array[0]}\|#{array[1]} #{array[2]}
\|#{array[3]}\|#{array[4]}|#{array[5]}|#{array[6]} #{array[7]}|
#{array[8]}|\n}
out_file.write(line_a)

the output is
000001|array0array1array2array3array4array5array6
| |||| ||
so basically my pipes and spaces are being appended to the end of my
string after the "\n"
any ideas?
 
A

Alex Gutteridge

ok, so it seems that it is not splitting the line into an array. so
what looks to be my array0-6 is actually just array[0]. So, my next
question is how do I split a line by tabs?

I was using array << line.split("\t"), but that isn't working.

Your split should work. Take care with your Array though. Array#<<
appends the object you give it to an Array. String#split returns an
Array so what you end up with is an Array which *contains* the Array
generated by split. Perhaps Array#+ is what you need?

irb(main):009:0> array = []
=> []
irb(main):010:0> string = "hi\tthere"
=> "hi\tthere"
irb(main):011:0> string.split("\t")
=> ["hi", "there"]
irb(main):012:0> array << string.split("\t")
=> [["hi", "there"]]
irb(main):013:0> array += string.split("\t")
=> [["hi", "there"], "hi", "there"]
Ideas?

thanks,

~Jeremy



I am trying to read 1 file then create another file from that 1. My
problem I am having is that my file is not being formated the same
way
I am telling it. So for example I have

line_a = %{#{i.to_s.rjust(6, "0")}|#{array[0]}\|#{array[1]} #{array
[2]}
\|#{array[3]}\|#{array[4]}|#{array[5]}|#{array[6]} #{array[7]}|
#{array[8]}|\n}

out_file.write(line_a)

the output is

000001|array0array1array2array3array4array5array6
| |||| ||

so basically my pipes and spaces are being appended to the end of my
string after the "\n"

Also look up Array#join to make your life easier.

Alex Gutteridge

Bioinformatics Center
Kyoto University
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,025
Latest member
Rigor4

Latest Threads

Top