[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

add a new line after, w/txtfile thats comma delimited

Mmcolli00 Mom

3/5/2009 3:08:00 PM

How do you add a new line after you have split a text file with
continuous values delimited with commas? Thanks in advance. MC

For example:
textfile contains: value1,value2,value3..

I want to output this file like this:
value1
value2
value3

#Here is my code:

File.open('temp.txt', 'r') do |temp|
arraym = [] #<--I am using array so that I can use array functions
..but I dont have to use array for this
temp.each_line do |line|
arraym= line.split(",",0)
b.puts ""
b.puts arraym # + "\n"
b.puts ""
end
end
--
Posted via http://www.ruby-....

14 Answers

Craig Demyanovich

3/5/2009 3:33:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

You should just be able to split each line on ',' which will give you an
array of values in the line. Then you can just puts the array. Here's a
little IRB session with some ideas.

>> line = "value1,value2,value3"
=> "value1,value2,value3"
>> values = line.split(',')
=> ["value1", "value2", "value3"]
>> values.each { |value| puts value }
value1
value2
value3
=> ["value1", "value2", "value3"]
>> puts values
value1
value2
value3
=> nil
>>

Regards,
Craig


--
Craig Demyanovich
Mutually Human Software
http://mutuall...

Mmcolli00 Mom

3/5/2009 3:36:00 PM

0

I tried that, it didn't work that way. I am not sure what is hidden in
the file that makes the values not separate. I'll keep digging. Thanks

--
Posted via http://www.ruby-....

Robert Klemme

3/5/2009 3:40:00 PM

0

On 05.03.2009 16:07, Mmcolli00 Mom wrote:
> How do you add a new line after you have split a text file with
> continuous values delimited with commas? Thanks in advance. MC
>
> For example:
> textfile contains: value1,value2,value3..
>
> I want to output this file like this:
> value1
> value2
> value3
>
> #Here is my code:
>
> File.open('temp.txt', 'r') do |temp|
> arraym = [] #<--I am using array so that I can use array functions
> .but I dont have to use array for this

This creation of the empty Array is completely superfluous as you do
nothing with this Array. You even do not need to declare the variable
outside of the block since you use it inside only and do not retain
values between iterations (lines).

> temp.each_line do |line|
> arraym= line.split(",",0)
> b.puts ""
> b.puts arraym # + "\n"
> b.puts ""
> end
> end

It can be as simple as

File.foreach "temp.txt" do |line|
puts "", line.split(/,/), ""
end

You can as well do

File.foreach "temp.txt" do |line|
printf "\n%s\n", line.gsub(/,/, "\n")
end

Kind regards

robert

terminate

3/5/2009 3:56:00 PM

0

I'm new to Ruby but I just read a book that had an example that is
similar to this. I've changed the code to see if I can solve this
problem and it seems to do it.

#code:

lines = File.readlines("temp.txt")
values = lines.split(/,/)

output_file = File.open('output.txt', 'w')
values.each do |line|
output_file.puts line
end

result should be:
value1
value2
value3
value4


On Mar 5, 10:07 am, Mmcolli00 Mom <mmc_coll...@yahoo.com> wrote:
> How do you add a new line after you have split a text file with
> continuous values delimited with commas? Thanks in advance. MC
>
> For example:
> textfile contains: value1,value2,value3..
>
> I want to output this file like this:
> value1
> value2
> value3
>
> #Here is my code:
>
> File.open('temp.txt', 'r') do |temp|
>      arraym = [] #<--I am using array so that I can use array functions
> .but I dont have to use array for this
>       temp.each_line do |line|
>          arraym=  line.split(",",0)
>          b.puts ""
>          b.puts arraym # + "\n"
>          b.puts ""
>       end
>  end
> --
> Posted viahttp://www.ruby-....

Reid Thompson

3/5/2009 4:03:00 PM

0

On Fri, 2009-03-06 at 00:43 +0900, Robert Klemme wrote:
> On 05.03.2009 16:07, Mmcolli00 Mom wrote:
> > How do you add a new line after you have split a text file with
> > continuous values delimited with commas? Thanks in advance. MC
> >
> > For example:
> > textfile contains: value1,value2,value3..
> >
> > I want to output this file like this:
> > value1
> > value2
> > value3
> >
> > #Here is my code:
> >
> > File.open('temp.txt', 'r') do |temp|
> > arraym = [] #<--I am using array so that I can use array functions
> > .but I dont have to use array for this
>
> This creation of the empty Array is completely superfluous as you do
> nothing with this Array. You even do not need to declare the variable
> outside of the block since you use it inside only and do not retain
> values between iterations (lines).
>
> > temp.each_line do |line|
> > arraym= line.split(",",0)
> > b.puts ""
> > b.puts arraym # + "\n"
> > b.puts ""
> > end
> > end
>
> It can be as simple as
>
> File.foreach "temp.txt" do |line|
> puts "", line.split(/,/), ""
> end
>
> You can as well do
>
> File.foreach "temp.txt" do |line|
> printf "\n%s\n", line.gsub(/,/, "\n")
> end
>
> Kind regards
>
> robert
>
rthompso@raker /tmp $ echo "value1,value2,value3" >/tmp/file.txt
rthompso@raker /tmp $ echo "value1,value2,value3" >/tmp/file2.txt
rthompso@raker /tmp $ echo "value1,value2,value3" >/tmp/file1.txt
rthompso@raker /tmp $ cat file*txt
value1,value2,value3
value1,value2,value3
value1,value2,value3
rthompso@raker /tmp $ ruby -p -i -e 'gsub(/,/, "\n")' file*.txt
rthompso@raker /tmp $ cat file*txt
value1
value2
value3
value1
value2
value3
value1
value2
value3


Mmcolli00 Mom

3/5/2009 4:06:00 PM

0

Do you know how to attach a string on the end of each value before
splitting into a new line? For instance...

value1 comments
value2 comments
value3 comments

I tried this using Robert's code, however, it put the comments on a
newline.

File.foreach "temp.txt" do |line|
puts "", line.split(/,/),"comments" + @usercomments
end



Thanks everyone for your help.

MC
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

3/5/2009 4:10:00 PM

0

On Thu, Mar 5, 2009 at 4:58 PM, terminate <andrewhoho@gmail.com> wrote:
> I'm new to Ruby but I just read a book that had an example that is
> similar to this. =A0I've changed the code to see if I can solve this
> problem and it seems to do it.
>
> #code:
>
> lines =3D File.readlines("temp.txt")
> values =3D lines.split(/,/)
>
> output_file =3D File.open('output.txt', 'w')
> values.each do |line|
> output_file.puts line
> end

Just a comment here. It's usually better to use the block form of open,
since that will take care of closing the file, even when an error happens:

File.open('output.txt','w') do |output_file|
#...
end

Jesus.

terminate

3/5/2009 4:14:00 PM

0

On Mar 5, 11:10 am, Jesús Gabriel y Galán <jgabrielyga...@gmail.com>
wrote:
> On Thu, Mar 5, 2009 at 4:58 PM, terminate <andrewh...@gmail.com> wrote:
> > I'm new to Ruby but I just read a book that had an example that is
> > similar to this.  I've changed the code to see if I can solve this
> > problem and it seems to do it.
>
> > #code:
>
> > lines = File.readlines("temp.txt")
> > values = lines.split(/,/)
>
> > output_file = File.open('output.txt', 'w')
> > values.each do |line|
> > output_file.puts line
> > end
>
> Just a comment here. It's usually better to use the block form of open,
> since that will take care of closing the file, even when an error happens:
>
> File.open('output.txt','w') do |output_file|
>  #...
> end
>
> Jesus.

Thanks for the clarification Jesus!

Mmcolli00 Mom

3/5/2009 4:23:00 PM

0

Do you know how to add a comment before each line splits values?

value1 comments
value2 comments
value3 comments

I tried this using Robert's code, however, it put the comments on a
newline.

File.foreach "temp.txt" do |line|
puts "", line.split(/,/),"comments" + @usercomments
end
--
Posted via http://www.ruby-....

Robert Klemme

3/5/2009 4:44:00 PM

0

On 05.03.2009 17:06, Mmcolli00 Mom wrote:
> Do you know how to attach a string on the end of each value before
> splitting into a new line? For instance...
>
> value1 comments
> value2 comments
> value3 comments
>
> I tried this using Robert's code, however, it put the comments on a
> newline.
>
> File.foreach "temp.txt" do |line|
> puts "", line.split(/,/),"comments" + @usercomments
> end

You'd rather choose the variant with gsub for this.

robert