I'm reading a table from a MySQL database and then processing it row by
row, stripping each line of certain characters ([, ], " and comma)
before writing it to a file. The code is running without errors, but
it's not stripping any of the characters as I would expect.
Here's the code:
#!/usr/bin/ruby
require 'mysql'
@bad_chars = '[],"'
begin
con = Mysql.new 'localhost', 'root', 'menagerie', 'haiku_archive'
rs = con.query("SELECT * FROM archive_2012")
n_rows = rs.num_rows
n_rows.times do
begin
file = File.open("archive.html", "a")
line = rs.fetch_row.to_s
line.gsub(/\[\]\,\"/,'')
file.write(line)
file.puts "<br>"
end
end
end
Executing the code results in an "archive.html" file with all of the
"stripped" characters still intact. Am I invoking the gsub method
incorrectly? Thanks in advance for any help.
on 2012-12-20 21:36
on 2012-12-20 22:24
On 21/12/2012, at 9:36 AM, Paul Mena <lists@ruby-forum.com> wrote: > > > Executing the code results in an "archive.html" file with all of the > "stripped" characters still intact. Am I invoking the gsub method > incorrectly? Thanks in advance for any help. There are two forms of gsub >> line = "this is a line with ***** in it" => "this is a line with ***** in it" >> line.gsub '*', '' => "this is a line with in it" >> line => "this is a line with ***** in it" >> line.gsub! '*', '' => "this is a line with in it" >> line => "this is a line with in it" The first form returns a new string, the second modifies the receiver. Also your regexp in the gsub probably doesn't do what you think it does. You might want something like line.gsub /[\[\],"]/, "**" Henry
on 2012-12-20 22:55
Hi, simply use String#delete or String#delete! http://www.ruby-doc.org/core-1.9.3/String.html#met... No need to fumble with regexes.
on 2012-12-20 23:41
Thanks for the responses. Unfortunately neither is having the desired result. Here is a "tail" of the outputted archive.html file: pablo@cochituate=> tail archive.html ["only Wednesday --<br>I stop fast-forwarding<br>between commercials<br>", "Sep-12-2012"]<br> ["last days of summer --<br>one more ghost story<br>around the fire<br>", "Sep-13-2012"]<br> ["maple-colored moon --<br>remembering<br>Mom’s pancakes<br>", "Sep-14-2012"]<br> ["Harvard Square station --<br>a Mozart sonata<br>between buses<br>", "Sep-14-2012"]<br> ["between buses<br>a Mozart sonata<br>", "Sep-14-2012"]<br> ["stiff sea breeze --<br>the drawbridge operator’s<br>bushy beard<br>", "Sep-15-2012"]<br> ["old sea port --<br>the tugboat’s<br>tattered flag<br>", "Sep-15-2012"]<br> ["dockside pub --<br>wondering where<br>the cormorant went<br>", "Sep-15-2012"]<br> ["dockside pub --<br>the bride-to-be’s<br>bright pink tiara<br>", "Sep-15-2012"]<br> ["after the break<br>somewhat less jumping<br>to the jump blues band<br>", "Sep-15-2012"]<br>
on 2012-12-21 02:13
Paul Mena wrote in post #1089769: > Thanks for the responses. Unfortunately neither is having the desired > result. Here is how computer programming forums work: 1) You post 15 lines or less of code that demonstrates your problem. 2) You post the actual output. 3) You state your desired/expected output. Your question has nothing to do with mysql, so your posted code should have no mysql lines in it.
on 2012-12-21 12:10
Paul Mena wrote in post #1089769: > Thanks for the responses. Unfortunately neither is having the desired > result. Here is a "tail" of the outputted archive.html file: The "tail" doesn't tell us anything, we need your *code*. I'm pretty sure you've again confused "gsub" and "gsub!" (or "delete" and "delete!") like in your first post.
on 2012-12-21 14:17
The code is the very first post in this thread, but I'll repeat some of
it here:
n_rows.times do
begin
file = File.open("archive.html", "a")
line = rs.fetch_row.to_s
line.gsub /\[\]\"\,/, ''
file.write(line)
file.puts "<br>"
end
end
The code is processing rows from a MySQL table as lines of text, one at
a time. The desired output would look something like this:
blah blah blah<br>meh<br>foo bar<br>Feb-12-2012<br>
Instead it looks like this:
["blah blah blah<br>meh<br>foo bar<br>", "Feb-12-2012<br>"]
on 2012-12-21 15:01
I'm talking about your *new* code where you used the above suggestions. You said that neither of them worked, so I'm asking you for the exact code. Henry told you that you need to use "gsub!" if you want the method to actually change the string (instead of returning a new string). I suggested using "delete!" as an alternative. So choose one of those two options, rewrite your code and try again.
on 2012-12-21 15:15
This finally worked, although it's certainly not elegant:
n_rows.times do
begin
file = File.open("archive.html", "a")
line = rs.fetch_row.to_s
line.gsub! '[', ''
line.gsub! '"', ''
line.gsub! ']', ''
line.gsub! ',', ''
file.write(line)
file.puts "<br>"
end
end
Thanks once again to everyone for their suggestions.
on 2012-12-21 15:34
Paul Mena wrote in post #1089856:
> Thanks once again to everyone for their suggestions.
It would be even better if you'd actually read them. :-/
What's the purpose of the "begin-end", by the way? This is not Pascal. A
"begin-end" block only makes sense in combination with "rescue" or
"ensure". Re-opening the file for every single row also doesn't really
make sense. Either open the file *before* the loop or collect the row
strings and then write them all at once.
File.open("archive.html", "a") do |file|
# I'm sure there's a better method for this, something like "each_row"
n_rows.times do
file << rs.fetch_row.to_s.delete('[]",')
file.puts '<br>'
end
end
on 2012-12-21 16:20
I did read and attempt to implement every suggestion made, but admittedly I'm new to Ruby and will make newbie mistakes (like mixing up gsub and gsub!). I started out as a FORTRAN developer back in the early 80s but have been a Sys Admin since the early 90s, and am still trying to wrap my mind around object-oriented programming.
on 2012-12-23 12:38
If you are trying to strip characters from a line in a web page file .html why may I ask are you using the "a" file opening mode? The "a" mode is to append to the bottom of a file.
on 2012-12-23 17:05
Alex, I'm appending to the "archive.html" line by line in a loop - that's why I opened it with the "a" mode. Cheers, Paul
on 2012-12-23 17:52
Instead of all those individual gsubs, why not this: irb(main):001:0> 'A["],a'.gsub(/\["\],/,'') => "Aa"
on 2012-12-23 21:01
Strangely, this didn't work for me: irb(main):001:0> '["the frustrated musician’s<br>mountain<br>of unsold CDs<br>", "Feb-12-2012"]<br>'.gsub(/\["\],/,'') => "[\"the frustrated musician’s<br>mountain<br>of unsold CDs<br>\", \"Feb-12-2012\"]<br>"
on 2012-12-24 00:21
Hello, it did not work, because his regular expression is describing a different pattern than you want: The \["\], looks for a [ (must be escaped in the regular expression, because it is a character with special meaning), followed by a ", followed by a ] (as with [), followed by a ,. His regular expression worked in his case, because he had ["], in his string. You want, as far as I can tell, remove all occurrences of those characters. The regular expression you want is similar: /[\[\]",]/ If you don't know what this does, here's an explanation: The [ denotes the beginning of a set, and the ] ends it. The set matches to any character inside of it, but only one (because I did not write a quantifier like * or + after it). So, this works: "A[Aaa[]aa, \"aa".gsub( /[\[\]",]/, '' ) # => "AAaaaa aaa" Alternatively, you could describe what you want with the regular expression /\[|\]|"|,/ since the pipe in regular expressions can be read as "or". Regards (Tested in Ruby 1.9.3p286; I don't know from the top of my head if the behavior would be any different in 1.8)
on 2012-12-24 02:05
Yes, my mistake, i was looking for that exact string instead of the group. It should be this: /[\["\],]/ that is / Start Regex [ Start group \ Escape next character [ look for open square bracket " Look for double quotes \ Escape next character ] Look for close square bracket , Look for comma ] End group / End Regex
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.