Forum: Ruby Pattern Matching Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-07-06 02:51
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-06 02:57
(Received via mailing list)
Hi --

On Fri, 6 Jul 2007, Ari Brown 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
0107ef1bc42d0626a706ca6af9a43060?d=identicon&s=25 Jos Backus (Guest)
on 2007-07-06 02:59
(Received via mailing list)
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 /^(.+)$/
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-06 03:03
(Received via mailing list)
Hi --

On Fri, 6 Jul 2007, Jos Backus wrote:

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

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


David
F3b02532d4cb4855881935c002389213?d=identicon&s=25 Morton Goldberg (Guest)
on 2007-07-06 05:29
(Received via mailing list)
On Jul 5, 2007, at 8:50 PM, Ari Brown 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
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-07-06 10:05
(Received via mailing list)
Hi,

Am Freitag, 06. Jul 2007, 12:27:40 +0900 schrieb Morton Goldberg:
> On Jul 5, 2007, at 8:50 PM, Ari Brown 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
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-07-06 17:44
(Received via mailing list)
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?

Thanks
-------------------------------------------------------|
~ Ari
crap my sig won't fit
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-07-06 17:54
(Received via mailing list)
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!

<http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

Bertram
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-06 18:09
(Received via mailing list)
Hi --

On Sat, 7 Jul 2007, Ari Brown wrote:

> 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
B36cd736c906455db541f7c8e2478145?d=identicon&s=25 diego scataglini (Guest)
on 2007-07-06 20:26
(Received via mailing list)
Actually the difference is exactly that!

Diego Scataglini

On Jul 6, 2007, at 11:52 AM, Bertram Scharpf <lists@bertram-
F3b02532d4cb4855881935c002389213?d=identicon&s=25 Morton Goldberg (Guest)
on 2007-07-06 20:55
(Received via mailing list)
On Jul 6, 2007, at 11:42 AM, Ari Brown 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-07-06 23:41
(Received via mailing list)
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 '

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
F3b02532d4cb4855881935c002389213?d=identicon&s=25 Morton Goldberg (Guest)
on 2007-07-07 00:23
(Received via mailing list)
On Jul 6, 2007, at 5:40 PM, Rob Biedenharn 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
This topic is locked and can not be replied to.