# Writing to a file help

**URL:** <https://www.ruby-forum.com/t/writing-to-a-file-help/117992>\
**Category:** Ruby\
**Created:** [October 23, 2007, 2:09am UTC](https://www.ruby-forum.com/t/writing-to-a-file-help/117992 "2007-10-23T02:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 23, 2007, 2:09am UTC](https://www.ruby-forum.com/t/writing-to-a-file-help/117992/1 "2007-10-23T02:09:32Z")

</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:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 23, 2007, 2:24am UTC](https://www.ruby-forum.com/t/writing-to-a-file-help/117992/2 "2007-10-23T02:24:43Z")

</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, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 23, 2007, 2:35am UTC](https://www.ruby-forum.com/t/writing-to-a-file-help/117992/3 "2007-10-23T02:35:49Z")

</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, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 23, 2007, 2:43am UTC](https://www.ruby-forum.com/t/writing-to-a-file-help/117992/4 "2007-10-23T02:43:25Z")

</div>

On 23 Oct 2007, at 09:24, removed\_email\_address@domain.invalid 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.

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”]

> > I am trying to read 1 file then create another file from that 1. My
> > 
> > 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 G.

Bioinformatics Center  
Kyoto University
