Pattern Matching Problem

A

Ari Brown

Ok. I have a major pattern matching problem. In essence, right now
the word i am trying to match is "hello", WITH double quotes around it.

Why isn't this working? It always hits the else part of my case-when
loop.

write_file = open('albuminfo.txt', 'w')
lines.each do |line|
puts line
case line
when line =~ /^(.+)$/
write_file.puts('"#{$1}"')
write_file.close
print '.'
else
print '$'
end
end

It doesn't want to match!

Please don't let me drown,
---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
 
D

dblack

Hi --

Ok. I have a major pattern matching problem. In essence, right now the word i
am trying to match is "hello", WITH double quotes around it.

Why isn't this working? It always hits the else part of my case-when loop.

write_file = open('albuminfo.txt', 'w')
lines.each do |line|
puts line
case line
when line =~ /^(.+)$/
write_file.puts('"#{$1}"')
write_file.close
print '.'
else
print '$'
end
end

It doesn't want to match!

You're using case/when wrong. You've got:

case line
when (some expression that is nil or an integer) ....

So you're really saying:

if (nil or integer) === line

You can either use a base case:

case
when line =~ /.../

or just use if/else, which to me seems more straightforward here.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Jos Backus

Ok. I have a major pattern matching problem. In essence, right now the word
i am trying to match is "hello", WITH double quotes around it.

Why isn't this working? It always hits the else part of my case-when loop.

write_file = open('albuminfo.txt', 'w')
lines.each do |line|
puts line
case line
when line =~ /^(.+)$/

when /^(.+)$/
 
M

Morton Goldberg

Ok. I have a major pattern matching problem. In essence, right now
the word i am trying to match is "hello", WITH double quotes around
it.

Why isn't this working? It always hits the else part of my case-
when loop.

write_file = open('albuminfo.txt', 'w')
lines.each do |line|
puts line
case line
when line =~ /^(.+)$/
write_file.puts('"#{$1}"')
write_file.close
print '.'
else
print '$'
end
end

Others have pointed out a problem with your case statement, but I
think there's another problem here -- your #{$1} substitution isn't
going to work in single-quoted string.

Regards, Morton
 
B

Bertram Scharpf

Hi,

Am Freitag, 06. Jul 2007, 12:27:40 +0900 schrieb Morton Goldberg:
Others have pointed out a problem with your case statement, but I
think there's another problem here -- your #{$1} substitution isn't
going to work in single-quoted string.

Further, the grouping is superfluous.

when /^.+$/ then
do_sth_with $&

will do.

As `line' will alwas contain a single newline character at
the strings end and as /./ will never match that, even

when /.+/ then

will do.

Bertram
 
A

Ari Brown

On Jul 5, 2007, at 11:27 PM, Morton Goldberg wrote:
Others have pointed out a problem with your case statement, but I
think there's another problem here -- your #{$1} substitution isn't
going to work in single-quoted string.

You're serious? I can't use variable substitution in a single quoted
string? I ran into that a while ago, but i just thought it was some
glitch on my part!

Why is that so? Whats the difference between single and double quoted
strings?

Thanks
-------------------------------------------------------|
~ Ari
crap my sig won't fit
 
D

dblack

Hi --

On Jul 5, 2007, at 11:27 PM, Morton Goldberg wrote:


You're serious? I can't use variable substitution in a single quoted string?
I ran into that a while ago, but i just thought it was some glitch on my
part!

Why is that so? Whats the difference between single and double quoted
strings?

Interpolation doesn't work in single quotes :) And a few other
things, especially:

"\n" newline
'\n' \n


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
M

Morton Goldberg

On Jul 5, 2007, at 11:27 PM, Morton Goldberg wrote:


You're serious? I can't use variable substitution in a single
quoted string? I ran into that a while ago, but i just thought it
was some glitch on my part!

Dead serious. However, the following may suggest a solution to you:

foo = 42
puts %["#{foo}"]
Why is that so?

I suspect it's to allow us to print strings verbatim.
Whats the difference between single and double quoted strings?

I believe single-quoted strings have only one special character --
the single quote.

Regards, Morton
 
R

Rob Biedenharn

On Jul 6, 2007, at 11:42 AM, Ari Brown wrote:
I suspect it's to allow us to print strings verbatim.


I believe single-quoted strings have only one special character --
the single quote.

Regards, Morton

And the backslash is a bit special:

'n' => "n"
'\'' => "'"
'\\' => "\\"
'\n' => "\\n" # because the \ is only special for \ and '

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
M

Morton Goldberg

And the backslash is a bit special:

'n' => "n"
'\'' => "'"
'\\' => "\\"
'\n' => "\\n" # because the \ is only special for \ and '

You're right, I should have said there are two special characters --
single quote and backslash.

Regards, Morton
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top