Need help massaging some strings

T

Taylor Strait

As a Rails developer, I've been learning Ruby on a need-to-know basis.
However, I now need to tap its power to batch alter some very large text
dumps and find myself a bit lost. Here is what I need to do:

Transform data in files like "alabama.txt":
* White Hall, Lowndes County
* Wilmer, Mobile County
* Wilsonville, Shelby County
* Wilton, Shelby County
* Winfield, Marion County
* Winterboro, Talladega County

...into this format:
White Hall
Wilmer
Wilsonville
Wilton
Winfield
Winterboro

I have created a method for doing this in "cleanup.rb":

def cleanup(state)
diskfile = File.new(state + "-cleaned.txt", "w")
$stdout = diskfile

IO.foreach(state + ".txt") do |line|
line.delete("\*")
line.strip!
temp = line.split(",")
temp.delete_at(1).to_s
puts temp
end

diskfile.close
$stdout = STDOUT
end

So I go into IRB and here is how my session goes:

irb(main):008:0> require 'cleanup'
=> true
irb(main):009:0> cleanup(alabama)
NameError: undefined local variable or method `alabama' for
main:Object
irb(main):010:0> cleanup
ArgumentError: wrong number of arguments (0 for 1)


So my method is loaded but isn't working. Could anyone offer any advice
on getting this thing running? Thanks in advance.
 
T

Taylor Strait

irb(main):009:0> cleanup(alabama)
NameError: undefined local variable or method `alabama' for

Got it. What a doofus - I just had to use this instead:

irb(main):009:0> cleanup("alabama")
 
E

Eric Hodel

As a Rails developer, I've been learning Ruby on a need-to-know basis.
However, I now need to tap its power to batch alter some very large
text
dumps and find myself a bit lost. Here is what I need to do:

Transform data in files like "alabama.txt":
* White Hall, Lowndes County
* Wilmer, Mobile County
* Wilsonville, Shelby County
* Wilton, Shelby County
* Winfield, Marion County
* Winterboro, Talladega County

...into this format:
White Hall
Wilmer
Wilsonville
Wilton
Winfield
Winterboro

I have created a method for doing this in "cleanup.rb":

def cleanup(state)
diskfile = File.new(state + "-cleaned.txt", "w")
$stdout = diskfile

IO.foreach(state + ".txt") do |line|
line.delete("\*")
line.strip!
temp = line.split(",")
temp.delete_at(1).to_s
puts temp
end

diskfile.close
$stdout = STDOUT
end

So I go into IRB and here is how my session goes:

irb(main):008:0> require 'cleanup'
=> true
irb(main):009:0> cleanup(alabama)
NameError: undefined local variable or method `alabama' for
main:Object

This is your clue. You didn't define the alabama variable or method.
irb(main):010:0> cleanup
ArgumentError: wrong number of arguments (0 for 1)

You have to pass one argument.
So my method is loaded but isn't working. Could anyone offer any
advice
on getting this thing running? Thanks in advance.

cleanup 'alabama'
 
W

William James

Taylor said:
As a Rails developer, I've been learning Ruby on a need-to-know basis.
However, I now need to tap its power to batch alter some very large text
dumps and find myself a bit lost. Here is what I need to do:

Transform data in files like "alabama.txt":
* White Hall, Lowndes County
* Wilmer, Mobile County
* Wilsonville, Shelby County
* Wilton, Shelby County
* Winfield, Marion County
* Winterboro, Talladega County

..into this format:
White Hall
Wilmer
Wilsonville
Wilton
Winfield
Winterboro

I have created a method for doing this in "cleanup.rb":

def cleanup(state)
diskfile = File.new(state + "-cleaned.txt", "w")
$stdout = diskfile

IO.foreach(state + ".txt") do |line|
line.delete("\*")
line.strip!
temp = line.split(",")
temp.delete_at(1).to_s
puts temp
end

diskfile.close
$stdout = STDOUT
end

So I go into IRB and here is how my session goes:

irb(main):008:0> require 'cleanup'
=> true
irb(main):009:0> cleanup(alabama)
NameError: undefined local variable or method `alabama' for
main:Object
irb(main):010:0> cleanup
ArgumentError: wrong number of arguments (0 for 1)

def cleanup(state)
File.open(state + "-cleaned.txt", "w") do |outfile|
IO.foreach(state + ".txt") do |line|
outfile.puts line[ /^[\s*]*(.*),[^,]*$/, 1 ]
end
end
end

Or:

def cleanup(state)
File.open(state + "-cleaned.txt", "w") { |out|
out.puts IO.readlines(state + ".txt").map{|line|
line[ /^[\s*]*(.*),[^,]*$/, 1 ] } }
end

Or even:

def cleanup(s)
open(s+"-cleaned.txt","w"){|o|o<<IO.read(s+".txt").
gsub(/^[ *]*|,.*$/,"")}
end
 
P

Paulo Köch

I think what the original author of this message was trying to say =20
was "Have you tryed RegExps?"
=3DP

Paulo Jorge Duarte K=F6ch
(e-mail address removed)


Taylor said:
As a Rails developer, I've been learning Ruby on a need-to-know =20
basis.
However, I now need to tap its power to batch alter some very =20
large text
dumps and find myself a bit lost. Here is what I need to do:

Transform data in files like "alabama.txt":
* White Hall, Lowndes County
* Wilmer, Mobile County
* Wilsonville, Shelby County
* Wilton, Shelby County
* Winfield, Marion County
* Winterboro, Talladega County

..into this format:
White Hall
Wilmer
Wilsonville
Wilton
Winfield
Winterboro

I have created a method for doing this in "cleanup.rb":

def cleanup(state)
diskfile =3D File.new(state + "-cleaned.txt", "w")
$stdout =3D diskfile

IO.foreach(state + ".txt") do |line|
line.delete("\*")
line.strip!
temp =3D line.split(",")
temp.delete_at(1).to_s
puts temp
end

diskfile.close
$stdout =3D STDOUT
end

So I go into IRB and here is how my session goes:

irb(main):008:0> require 'cleanup'
=3D> true
irb(main):009:0> cleanup(alabama)
NameError: undefined local variable or method `alabama' for
main:Object
irb(main):010:0> cleanup
ArgumentError: wrong number of arguments (0 for 1)

def cleanup(state)
File.open(state + "-cleaned.txt", "w") do |outfile|
IO.foreach(state + ".txt") do |line|
outfile.puts line[ /^[\s*]*(.*),[^,]*$/, 1 ]
end
end
end

Or:

def cleanup(state)
File.open(state + "-cleaned.txt", "w") { |out|
out.puts IO.readlines(state + ".txt").map{|line|
line[ /^[\s*]*(.*),[^,]*$/, 1 ] } }
end

Or even:

def cleanup(s)
open(s+"-cleaned.txt","w"){|o|o<<IO.read(s+".txt").
gsub(/^[ *]*|,.*$/,"")}
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top