Pattern Matching Problem

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

Hi –

On Fri, 6 Jul 2007, Ari B. wrote:

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

On Fri, Jul 06, 2007 at 09:50:13AM +0900, Ari B. wrote:

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 /^(.+)$/

Hi –

On Fri, 6 Jul 2007, Jos B. wrote:

when line =~ /^(.+)$/

when /^(.+)$/

Or that :slight_smile: (see my post which mentioned two solutions but not this
one)

David

On Jul 5, 2007, at 8:50 PM, Ari B. wrote:

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

On Jul 5, 2007, at 11:27 PM, Morton G. 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

Hi,

Am Freitag, 06. Jul 2007, 12:27:40 +0900 schrieb Morton G.:

On Jul 5, 2007, at 8:50 PM, Ari B. wrote:

when line =~ /^(.+)$/
write_file.puts(’"#{$1}"’)

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

Hi,

Am Samstag, 07. Jul 2007, 00:42:24 +0900 schrieb Ari B.:

You’re serious? I can’t use variable substitution in a single quoted
string? […]

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

RTFM!

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S2

Bertram

Actually the difference is exactly that!

Diego Scataglini

On Jul 6, 2007, at 11:52 AM, Bertram S. <lists@bertram-

Hi –

On Sat, 7 Jul 2007, Ari B. wrote:

part!

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

Interpolation doesn’t work in single quotes :slight_smile: And a few other
things, especially:

“\n” newline
‘\n’ \n

David

On Jul 6, 2007, at 11:42 AM, Ari B. wrote:

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

On Jul 6, 2007, at 2:54 PM, Morton G. wrote:

On Jul 6, 2007, at 11:42 AM, Ari B. wrote:
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

And the backslash is a bit special:

‘n’ => “n”
‘'’ => “'”
‘\’ => “\”
‘\n’ => “\n” # because the \ is only special for \ and ’

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Jul 6, 2007, at 5:40 PM, Rob B. wrote:

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