# Writing to a file help

**URL:** <https://rubytalk.org/t/writing-to-a-file-help/41587>\
**Category:** ruby-talk\
**Created:** [23 October 2007 00:08 UTC](https://rubytalk.org/t/writing-to-a-file-help/41587 "2007-10-23T00:08:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [23 October 2007 00:08 UTC](https://rubytalk.org/t/writing-to-a-file-help/41587/1 "2007-10-23T00:08:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [23 October 2007 00:24 UTC](https://rubytalk.org/t/writing-to-a-file-help/41587/2 "2007-10-23T00:24:05Z")

</div>

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

> **···**
>
> On Oct 22, 5:08 pm, "JeremyWoert...@gmail.com" \<JeremyWoert...@gmail.com\> wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [23 October 2007 00:35 UTC](https://rubytalk.org/t/writing-to-a-file-help/41587/3 "2007-10-23T00:35:18Z")

</div>

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

> **···**
>
> On Oct 22, 5:24 pm, "JeremyWoert...@gmail.com" \<JeremyWoert...@gmail.com\> wrote:
> 
> > 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
> > 
> > On Oct 22, 5:08 pm, "JeremyWoert...@gmail.com" \> \> \<JeremyWoert...@gmail.com\> wrote:  
> > \> 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

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alex\_Gutteridge](https://rubytalk.org/u/Alex_Gutteridge)\
**Post date:** [23 October 2007 00:42 UTC](https://rubytalk.org/t/writing-to-a-file-help/41587/4 "2007-10-23T00:42:28Z")

</div>

> 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

> **···**
>
> On 23 Oct 2007, at 09:24, JeremyWoertink@gmail.com wrote:
> 
> > On Oct 22, 5:08 pm, "JeremyWoert...@gmail.com" \> \<JeremyWoert...@gmail.com\> wrote:
