Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2009, 10:14 AM
nyteshade nyteshade is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: oh, usa
Posts: 13 nyteshade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 46 m 13 sec
Reputation Power: 0
Output to file as 3 fields per row

I'm trying to process an input file that looks like:

AL,Alabama,Montgomery
AK,Alaska,Juneau
AZ,Arizona,Phoenix
AR,Arkansas,Little Rock

... and out a file that looks like:

"AL","Alabama","Montgomery"
"AK","Alaska","Juneau"
"AZ","Arizona","Phoenix"
"AR","Arkansas","Little Rock"

The code so far looks like this:

myFile = Array.new
myFile =File.readlines("C:/my_ruby_code/list_of_state_capitals_wState.txt")
myFile.collect! {|line| line.strip}

myFile.each do |line|
puts line.split(/\,/).inspect
myFile = line.split(/\,/).collect! {|element| '"'+element+'"'}
puts myFile
end

File.open("C:/my_ruby_code/output_file.txt", "w") do |line|
line.puts myFile
end

The only output I'm getting is this (the last line):

"WY"
"Wyoming"
"Cheyenne"

I'm a ruby nube so please, be kind. I know that line.split guy is separating each abbreviation, state and city into separate array slots but my brain hurts from trying to figure out a good one-liner like:

oneLine = 3.times {line.split(/\,/).collect! {|element| '"'+element+'"'}}

But, oneLine just ends up containing the number 3! Help please, thanks all!

Reply With Quote
  #2  
Old November 22nd, 2009, 11:21 AM
h4z3 h4z3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 41 h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 17 h 50 m 13 sec
Reputation Power: 41
Quote:
Originally Posted by nyteshade
myFile = Array.new
myFile =File.readlines("C:/my_ruby_code/list_of_state_capitals_wState.txt")
myFile.collect! {|line| line.strip}

First of all, you don't need to pre-declare the myFile Array. File::readlines returns an Array so the first line isn't necessary.

The second and third line could be joined to create a more concise block. For example:
Code:
myFile = File.readlines("states.txt").collect {|line| line.strip }


Quote:
Originally Posted by nyteshade
myFile.each do |line|
puts line.split(/\,/).inspect
myFile = line.split(/\,/).collect! {|element| '"'+element+'"'}
puts myFile
end

File.open("C:/my_ruby_code/output_file.txt", "w") do |line|
line.puts myFile
end

In this block of code, why are you reassigning the myFile variable?
Also, Array#collect should be used over Array#collect! the exclamation mark tells Ruby to modify the contents of the Object it's called on. This isn't always what's returned.

If I were you I would read the lines of the original file, then open another file Object for output (this could be the same file) and re-print the new data to that file. As for formatting the new lines you could use String#insert to add the new double quotes.

For example in IRB:
>> puts "AR,Arkansas,Little Rock".split(',').map {|x| x.insert(0, '"').insert(-1, '"') }.join(',')
"AR","Arkansas","Little Rock"

A full example:
Code:
#!/usr/bin/ruby -w

def format(str)
  str.split(',').map {|text| text.strip.insert(0, '"').insert(-1, '"') }.join(',')
end

File.open('output.txt', 'w') do |file|
  DATA.each do |line|
    newline = format(line)
    
    file.puts newline
  end
end

__END__
AL,Alabama,Montgomery
AK,Alaska,Juneau
AZ,Arizona,Phoenix
AR,Arkansas,Little Rock


You could also use gsub, or many other means of inserting the double quotes into the lines. Obviously this code could be simplified a lot so if any of it confuses you, just break it down and use as many lines as you need too rather than throwing it all on one like I have.

On another note, please use code tags when posting. Thanks :-)

Reply With Quote
  #3  
Old November 22nd, 2009, 11:29 AM
h4z3 h4z3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 41 h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 17 h 50 m 13 sec
Reputation Power: 41
Quote:
Originally Posted by nyteshade
oneLine = 3.times {line.split(/\,/).collect! {|element| '"'+element+'"'}}

But, oneLine just ends up containing the number 3! Help please, thanks all!


Forgot to add, this is because Integer#times returns an Integer (the amount of times the block has been iterated). In this case 3, so if 3.times {} returns 3 then that's the value you're assigning to the oneLine variable

Reply With Quote
  #4  
Old November 22nd, 2009, 11:55 AM
nyteshade nyteshade is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: oh, usa
Posts: 13 nyteshade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 46 m 13 sec
Reputation Power: 0
okie-dokie

Keep in mind I'm a nube to Ruby, it'll take me awhile to confirm what you've done. Thanks for the detail. I've only just finished chapter 5 of Peter Cooper's Beginning Ruby, sooo, I can't even spell 'map' yet; the method you created is more than I know so I need to read and test, bbl. Thanks again!

Reply With Quote
  #5  
Old November 22nd, 2009, 12:35 PM
nyteshade nyteshade is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: oh, usa
Posts: 13 nyteshade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 46 m 13 sec
Reputation Power: 0
regarding the irb line

Code:
>> puts "AR,Arkansas,Little Rock".split(',').map {|x| x.insert(0, '"').insert(-1, '"') }.join(',')
"AR","Arkansas","Little Rock"


Quote:
What this is doing is taking the string, splitting it on the comma, mapping it over to |x| in the code block, performing the insert method by appending a double-quote at the beginning (0) and end (-1) of each 'map split element' and finally joining each 'map split element' using a comma. Yes? Ok, then I got that part and it works in IRB. I guess this confirms that your format method should also work. Let me try that now...bbl.





Quote:
Originally Posted by h4z3
First of all, you don't need to pre-declare the myFile Array. File::readlines returns an Array so the first line isn't necessary.

The second and third line could be joined to create a more concise block. For example:
Code:
myFile = File.readlines("states.txt").collect {|line| line.strip }



In this block of code, why are you reassigning the myFile variable?
Also, Array#collect should be used over Array#collect! the exclamation mark tells Ruby to modify the contents of the Object it's called on. This isn't always what's returned.

If I were you I would read the lines of the original file, then open another file Object for output (this could be the same file) and re-print the new data to that file. As for formatting the new lines you could use String#insert to add the new double quotes.

For example in IRB:
>> puts "AR,Arkansas,Little Rock".split(',').map {|x| x.insert(0, '"').insert(-1, '"') }.join(',')
"AR","Arkansas","Little Rock"

A full example:
Code:
#!/usr/bin/ruby -w

def format(str)
  str.split(',').map {|text| text.strip.insert(0, '"').insert(-1, '"') }.join(',')
end

File.open('output.txt', 'w') do |file|
  DATA.each do |line|
    newline = format(line)
    
    file.puts newline
  end
end

__END__
AL,Alabama,Montgomery
AK,Alaska,Juneau
AZ,Arizona,Phoenix
AR,Arkansas,Little Rock


You could also use gsub, or many other means of inserting the double quotes into the lines. Obviously this code could be simplified a lot so if any of it confuses you, just break it down and use as many lines as you need too rather than throwing it all on one like I have.

On another note, please use code tags when posting. Thanks :-)

Reply With Quote
  #6  
Old November 22nd, 2009, 01:21 PM
nyteshade nyteshade is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: oh, usa
Posts: 13 nyteshade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 46 m 13 sec
Reputation Power: 0
got it - understand it!

Quote:
Originally Posted by nyteshade
Code:
>> puts "AR,Arkansas,Little Rock".split(',').map {|x| x.insert(0, '"').insert(-1, '"') }.join(',')
"AR","Arkansas","Little Rock"


Quote:
Ok, I got it to work for me. I see how (and understand) you stacked those methods, sweet. Here's what I did (I'll experiment with gsub later). Thanks again.


Code:
def format(str)
  str.split(',').map {|text| text.strip.insert(0, '"').insert(-1, '"') }.join(',')
end
outFile = File.open("C:/my_ruby_code/output_file.txt", 'w') 
File.open("C:/my_ruby_code/list_of_state_capitals_wState.txt", 'r') do |myFile|
    myFile.each do |line|
      newline = format(line)
      outFile.puts newline
    end
end

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Output to file as 3 fields per row

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap