[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Writing to a file help

Jeremy Woertink

10/23/2007 12:09:00 AM

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


3 Answers

Jeremy Woertink

10/23/2007 12:24:00 AM

0

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


Jeremy Woertink

10/23/2007 12:35:00 AM

0

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


Alex Gutteridge

10/23/2007 12:42:00 AM

0

On 23 Oct 2007, at 09:24, JeremyWoertink@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.

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
>
>
>
> 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"
>>

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

Alex Gutteridge

Bioinformatics Center
Kyoto University