[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Pattern Matching Problem

Ari Brown

7/6/2007 12:50:00 AM

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




12 Answers

dblack

7/6/2007 12:57:00 AM

0

Jos Backus

7/6/2007 12:58:00 AM

0

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

> write_file.puts('"#{$1}"')
> write_file.close
> print '.'
> else
> print '$'
> end
> end
>
> It doesn't want to match!

--
Jos Backus
jos at catnook.com

dblack

7/6/2007 1:03:00 AM

0

Morton Goldberg

7/6/2007 3:28:00 AM

0

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

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



Bertram Scharpf

7/6/2007 8:02:00 AM

0

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



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

Ari Brown

7/6/2007 3:42:00 PM

0


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


Bertram Scharpf

7/6/2007 3:53:00 PM

0

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/tut_stdtypes.h...

Bertram


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

dblack

7/6/2007 4:08:00 PM

0

diego scataglini

7/6/2007 6:24:00 PM

0

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!
>
> <http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtyp...
> >
>
> Bertram
>
>
> --
> Bertram Scharpf
> Stuttgart, Deutschland/Germany
> http://www.bertram-...
>

Morton Goldberg

7/6/2007 6:54:00 PM

0

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

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