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 --

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

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, 6 Jul 2007, Ari Brown wrote:

--
* 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)

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

···

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

    write_file.puts('"#{$1}"')
    write_file.close
    print '.'
  else
    print '$'
  end
end

It doesn't want to match!

--
Jos Backus
jos at catnook.com

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 8:50 PM, Ari Brown 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 =~ /^(.+)$/
    write_file.puts('"#{$1}"')
    write_file.close
    print '.'
  else
    print '$'
  end
end

Hi --

···

On Fri, 6 Jul 2007, Jos Backus wrote:

On Fri, Jul 06, 2007 at 09:50:13AM +0900, Ari Brown 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 /^(.+)$/

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

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)

Hi,

> 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

···

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

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

<snip>

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

···

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

Hi,

···

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

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!

<Programming Ruby: The Pragmatic Programmer's Guide;

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi --

···

On Sat, 7 Jul 2007, Ari Brown wrote:

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

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?

Interpolation doesn't work in single quotes :slight_smile: 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)

<snip>

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!

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 11:42 AM, Ari Brown wrote:

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

Actually the difference is exactly that!

Diego Scataglini

···

On Jul 6, 2007, at 11:52 AM, Bertram Scharpf <lists@bertram- scharpf.de> wrote:

Hi,

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

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!

<Programming Ruby: The Pragmatic Programmer's Guide;

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

And the backslash is a bit special:

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

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

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

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

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

Regards, Morton

···

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

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

On Jul 6, 2007, at 11:42 AM, Ari Brown 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 '