Question: formatting strings from array for text files

I'm not sure who to have an array format correctly when written to a text file.
Doing something like myarray = ['five','four','three','two','one']
works almost correctly but
myarray = ['.././*.docs'] seems to write them out without linebreaks.

So while I can figure out how to print them nicely using some iterator
to the screen
Using,
      File.open filename, 'w' do |doc|
      doc.write myarray
      end
I can't seem to figure out a way to format the strings correctly.

Stuart

I'm not sure who to have an array format correctly when written to a text file.

What does "correctly" mean?

Doing something like myarray = ['five','four','three','two','one']
works almost correctly but
myarray = ['.././*.docs'] seems to write them out without linebreaks.

This is not a print statement. What did you do to actually print it?

So while I can figure out how to print them nicely using some iterator
to the screen
Using,
      File.open filename, 'w' do |doc|
      doc.write myarray
      end
I can't seem to figure out a way to format the strings correctly.

Try this:

doc.write myarray.join ", "

Depends on what you mean by "proper output".

Kind regards

robert

···

2006/7/6, Dark Ambient <sambient@gmail.com>:

--
Have a look: Robert K. | Flickr

I must have not explained myself well.
Right now my file looks like this:

c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
..........i.e. no line breaks

I want it took look like this:
c:/dir1/doc1.doc
c;/dir1/doc3.doc
c:/dir1/doc4.doc
c:/dir1/doc5.doc
..........

Stuart

···

On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:

2006/7/6, Dark Ambient <sambient@gmail.com>:
> I'm not sure who to have an array format correctly when written to a text file.

What does "correctly" mean?

> Doing something like myarray = ['five','four','three','two','one']
> works almost correctly but
> myarray = ['.././*.docs'] seems to write them out without linebreaks.

This is not a print statement. What did you do to actually print it?

> So while I can figure out how to print them nicely using some iterator
> to the screen
> Using,
> File.open filename, 'w' do |doc|
> doc.write myarray
> end
> I can't seem to figure out a way to format the strings correctly.

Try this:

doc.write myarray.join ", "

Depends on what you mean by "proper output".

Kind regards

robert

--
Have a look: Robert K. | Flickr

Dark Ambient wrote:

I must have not explained myself well.
Right now my file looks like this:

c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
..........i.e. no line breaks

I want it took look like this:
c:/dir1/doc1.doc
c;/dir1/doc3.doc
c:/dir1/doc4.doc
c:/dir1/doc5.doc
..........

Stuart

> I'm not sure who to have an array format correctly when written to a text file.

What does "correctly" mean?

> Doing something like myarray = ['five','four','three','two','one']
> works almost correctly but
> myarray = ['.././*.docs'] seems to write them out without linebreaks.

This is not a print statement. What did you do to actually print it?

> So while I can figure out how to print them nicely using some iterator
> to the screen
> Using,
> File.open filename, 'w' do |doc|
> doc.write myarray
> end
> I can't seem to figure out a way to format the strings correctly.

Try this:

doc.write myarray.join ", "

Depends on what you mean by "proper output".

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fussel-foto/

Try using 'doc.puts' instead of 'doc.write'. 'puts' will automatically add line breaks, whereas 'write' will not.

-Justin

···

On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:

2006/7/6, Dark Ambient <sambient@gmail.com>:

Thank you that was a huge improvement, still battling a few more format issues:
when I open the file:
It has the 3 hypens as the first line and a hyphen before every file
name. Can't I get rid of those ?

···

---
- c:/dir1/doc1.doc
- c:/dir1/doc2.doc
- c:/dir1/doc3.doc
- c:/dir1/doc4.doc
- c:/dir1/doc5.doc
- c:/dir1/doc6.doc

Stuart
On 7/6/06, Justin Collins <collinsj@seattleu.edu> wrote:

Dark Ambient wrote:
> I must have not explained myself well.
> Right now my file looks like this:
>
> c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
> ..........i.e. no line breaks
>
> I want it took look like this:
> c:/dir1/doc1.doc
> c;/dir1/doc3.doc
> c:/dir1/doc4.doc
> c:/dir1/doc5.doc
> ..........
>
> Stuart
>
> On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:
>> 2006/7/6, Dark Ambient <sambient@gmail.com>:
>> > I'm not sure who to have an array format correctly when written to
>> a text file.
>>
>> What does "correctly" mean?
>>
>> > Doing something like myarray = ['five','four','three','two','one']
>> > works almost correctly but
>> > myarray = ['.././*.docs'] seems to write them out without linebreaks.
>>
>> This is not a print statement. What did you do to actually print it?
>>
>> > So while I can figure out how to print them nicely using some iterator
>> > to the screen
>> > Using,
>> > File.open filename, 'w' do |doc|
>> > doc.write myarray
>> > end
>> > I can't seem to figure out a way to format the strings correctly.
>>
>> Try this:
>>
>> doc.write myarray.join ", "
>>
>> Depends on what you mean by "proper output".
>>
>> Kind regards
>>
>> robert
>>
>> --
>> Have a look: Robert K. | Flickr
>>
>
Try using 'doc.puts' instead of 'doc.write'. 'puts' will automatically
add line breaks, whereas 'write' will not.

-Justin

I'd think there has to be a way.

···

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:

Thank you that was a huge improvement, still battling a few more format issues:
when I open the file:
It has the 3 hypens as the first line and a hyphen before every file
name. Can't I get rid of those ?

---
- c:/dir1/doc1.doc
- c:/dir1/doc2.doc
- c:/dir1/doc3.doc
- c:/dir1/doc4.doc
- c:/dir1/doc5.doc
- c:/dir1/doc6.doc

Stuart
On 7/6/06, Justin Collins <collinsj@seattleu.edu> wrote:
> Dark Ambient wrote:
> > I must have not explained myself well.
> > Right now my file looks like this:
> >
> > c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
> > ..........i.e. no line breaks
> >
> > I want it took look like this:
> > c:/dir1/doc1.doc
> > c;/dir1/doc3.doc
> > c:/dir1/doc4.doc
> > c:/dir1/doc5.doc
> > ..........
> >
> > Stuart
> >
> > On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> 2006/7/6, Dark Ambient <sambient@gmail.com>:
> >> > I'm not sure who to have an array format correctly when written to
> >> a text file.
> >>
> >> What does "correctly" mean?
> >>
> >> > Doing something like myarray = ['five','four','three','two','one']
> >> > works almost correctly but
> >> > myarray = ['.././*.docs'] seems to write them out without linebreaks.
> >>
> >> This is not a print statement. What did you do to actually print it?
> >>
> >> > So while I can figure out how to print them nicely using some iterator
> >> > to the screen
> >> > Using,
> >> > File.open filename, 'w' do |doc|
> >> > doc.write myarray
> >> > end
> >> > I can't seem to figure out a way to format the strings correctly.
> >>
> >> Try this:
> >>
> >> doc.write myarray.join ", "
> >>
> >> Depends on what you mean by "proper output".
> >>
> >> Kind regards
> >>
> >> robert
> >>
> >> --
> >> Have a look: Robert K. | Flickr
> >>
> >
> Try using 'doc.puts' instead of 'doc.write'. 'puts' will automatically
> add line breaks, whereas 'write' will not.
>
> -Justin
>

Dark Ambient wrote:

I'd think there has to be a way.

Thank you that was a huge improvement, still battling a few more format issues:
when I open the file:
It has the 3 hypens as the first line and a hyphen before every file
name. Can't I get rid of those ?

---
- c:/dir1/doc1.doc
- c:/dir1/doc2.doc
- c:/dir1/doc3.doc
- c:/dir1/doc4.doc
- c:/dir1/doc5.doc
- c:/dir1/doc6.doc

How are you building the array? What you are seeing as output must be what's in the array, unless you are adding those hyphens at some point. Maybe do a

p myarray

to see exactly what is in the array.

Of course, you could just do some string parsing and remove the hyphens.

-Justin

···

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:

Okay, I made some changes to my code. I was using .to_yaml, without
it, the extra characters are now gone.

However, I'm writing this file successfully yet Ruby is returning false.
So, perhaps someone would know why ? (btw, went from .docs to .mp3's)

songs = Dir['c:/mp3/*.mp3']
filename = 'songs.m3u'

File.open filename, 'w' do |f|
  f.puts songs
end

readlist = File.read filename

puts(readlist == songs)

Stuart

···

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:

I'd think there has to be a way.

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:
> Thank you that was a huge improvement, still battling a few more format issues:
> when I open the file:
> It has the 3 hypens as the first line and a hyphen before every file
> name. Can't I get rid of those ?
>
> ---
> - c:/dir1/doc1.doc
> - c:/dir1/doc2.doc
> - c:/dir1/doc3.doc
> - c:/dir1/doc4.doc
> - c:/dir1/doc5.doc
> - c:/dir1/doc6.doc
>
> Stuart
> On 7/6/06, Justin Collins <collinsj@seattleu.edu> wrote:
> > Dark Ambient wrote:
> > > I must have not explained myself well.
> > > Right now my file looks like this:
> > >
> > > c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
> > > ..........i.e. no line breaks
> > >
> > > I want it took look like this:
> > > c:/dir1/doc1.doc
> > > c;/dir1/doc3.doc
> > > c:/dir1/doc4.doc
> > > c:/dir1/doc5.doc
> > > ..........
> > >
> > > Stuart
> > >
> > > On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> > >> 2006/7/6, Dark Ambient <sambient@gmail.com>:
> > >> > I'm not sure who to have an array format correctly when written to
> > >> a text file.
> > >>
> > >> What does "correctly" mean?
> > >>
> > >> > Doing something like myarray = ['five','four','three','two','one']
> > >> > works almost correctly but
> > >> > myarray = ['.././*.docs'] seems to write them out without linebreaks.
> > >>
> > >> This is not a print statement. What did you do to actually print it?
> > >>
> > >> > So while I can figure out how to print them nicely using some iterator
> > >> > to the screen
> > >> > Using,
> > >> > File.open filename, 'w' do |doc|
> > >> > doc.write myarray
> > >> > end
> > >> > I can't seem to figure out a way to format the strings correctly.
> > >>
> > >> Try this:
> > >>
> > >> doc.write myarray.join ", "
> > >>
> > >> Depends on what you mean by "proper output".
> > >>
> > >> Kind regards
> > >>
> > >> robert
> > >>
> > >> --
> > >> Have a look: Robert K. | Flickr
> > >>
> > >
> > Try using 'doc.puts' instead of 'doc.write'. 'puts' will automatically
> > add line breaks, whereas 'write' will not.
> >
> > -Justin
> >
>

Dark Ambient wrote:

Okay, I made some changes to my code. I was using .to_yaml, without
it, the extra characters are now gone.

However, I'm writing this file successfully yet Ruby is returning false.
So, perhaps someone would know why ? (btw, went from .docs to .mp3's)

songs = Dir['c:/mp3/*.mp3']
filename = 'songs.m3u'

File.open filename, 'w' do |f|
f.puts songs
end

readlist = File.read filename

puts(readlist == songs)

Stuart

I'd think there has to be a way.

> Thank you that was a huge improvement, still battling a few more format issues:
> when I open the file:
> It has the 3 hypens as the first line and a hyphen before every file
> name. Can't I get rid of those ?
>
> ---
> - c:/dir1/doc1.doc
> - c:/dir1/doc2.doc
> - c:/dir1/doc3.doc
> - c:/dir1/doc4.doc
> - c:/dir1/doc5.doc
> - c:/dir1/doc6.doc
>
> Stuart
> > Dark Ambient wrote:
> > > I must have not explained myself well.
> > > Right now my file looks like this:
> > >
> > > c:/dir1/doc1.docc;/dir1/doc3.docc:/dir1/doc4.docc:/dir1/doc5.doc
> > > ..........i.e. no line breaks
> > >
> > > I want it took look like this:
> > > c:/dir1/doc1.doc
> > > c;/dir1/doc3.doc
> > > c:/dir1/doc4.doc
> > > c:/dir1/doc5.doc
> > > ..........
> > >
> > > Stuart
> > >
> > >> > I'm not sure who to have an array format correctly when written to
> > >> a text file.
> > >>
> > >> What does "correctly" mean?
> > >>
> > >> > Doing something like myarray = ['five','four','three','two','one']
> > >> > works almost correctly but
> > >> > myarray = ['.././*.docs'] seems to write them out without linebreaks.
> > >>
> > >> This is not a print statement. What did you do to actually print it?
> > >>
> > >> > So while I can figure out how to print them nicely using some iterator
> > >> > to the screen
> > >> > Using,
> > >> > File.open filename, 'w' do |doc|
> > >> > doc.write myarray
> > >> > end
> > >> > I can't seem to figure out a way to format the strings correctly.
> > >>
> > >> Try this:
> > >>
> > >> doc.write myarray.join ", "
> > >>
> > >> Depends on what you mean by "proper output".
> > >>
> > >> Kind regards
> > >>
> > >> robert
> > >>
> > >> --
> > >> Have a look: Robert K. | Flickr
> > >>
> > >
> > Try using 'doc.puts' instead of 'doc.write'. 'puts' will automatically
> > add line breaks, whereas 'write' will not.
> >
> > -Justin
> >
>

You are comparing a string to an array. File.read returns a string. Try something like:

readlist = File.readlines(filename).map { |line| line.chomp}

File.readlines will return an array with an element for each line. But they will then have the end-of-line character attached (added when we wrote the file), so we have to remove those with chomp.

-Justin

···

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:

On 7/6/06, Dark Ambient <sambient@gmail.com> wrote:
> On 7/6/06, Justin Collins <collinsj@seattleu.edu> wrote:
> > > On 7/6/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> > >> 2006/7/6, Dark Ambient <sambient@gmail.com>: