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.
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 21/12/2012, at 9:36 AM, Paul Mena <lists@ruby-forum.com> wrote:
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:
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.
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
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
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.
···
Date: Fri, 21 Dec 2012 23:15:52 +0900
From: lists@ruby-forum.com
Subject: Re: trying to strip characters from a line
To: ruby-talk@ruby-lang.org
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.
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[Aaaaa, \"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)