Check if char in string?

Almost seems that you try to prove Robert's theory :frowning:
Robert (the other one).

···

On Thu, May 8, 2008 at 4:10 AM, globalrev <skanemupp@yahoo.se> wrote:

I have a question:

If you are only testing if a single character is in a string 'aeiouy'
what are the advantages of using a regular expression instead of include?
(or index).

If I understand correctly what the original poster is trying to do
(caveat: I'm not at all sure that I do), it is something like:

str = 'you muppet'
0.upto( str.size - 1 ) do | n |
  if 'aeiouy'.include?( str[ n, 1 ] ) then
    puts 'vowel'
  else
    puts 'consonant'
  end
end

Regular expressions will work for more complicated tests
(which is why I ought to learn them!),
but do they have an advantage for something like this,
other than getting used to them?

aa = "Hello World"
if(aa.include?('o'))
    puts 'yes'
end

···

On May 8, 11:27 am, Jim Cochrane <allergic-to-s...@no-spam- allowed.org> wrote:

On 2008-05-08, globalrev <skanem...@yahoo.se> wrote:

> this way:
> if str.chr =~ /[aeiouy]/
> i can see if a char is any of the chars aeioyu

> but i want to see if its not...
> if str.chr !=~ /[aeiouy]/

how about something like:

if not str.chr =~ /[aeiouy]/ then print "YO\n" end

OR

if str.chr !~ /[aeiouy]/ then print "YO\n" end

--

ty all for the help. haesob park did what iw anted.

this is the program i wrote, it is an encrypter/decrypter for the
robbers language(rövarspråket in swedish), its from the book Kalle
Blomkvist by late and very famous author Astrid Lindgren.
you add o + the consonant, nothing is done to vowels.
so d becomes dod, a is just a.

dad is therefore dodadod, super is sosupoperor etc.

puts "Enter sentence to encrypt: "
str = gets

enc = ""
for x in (0..str.length()-1)
  if str[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
      enc = enc + str[x].chr + "o" + str[x].chr
  else
      enc = enc +str[x].chr
  end
end

print enc

puts "Enter code to decrypt: "
code = gets

x = 0
dec = ""
while (x<code.length())
  dec = dec + code[x].chr
  if code[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
    x = x + 2
  end
  x = x + 1
end

print dec

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as "a"?

Hi,

globalrev wrote:

ty all for the help. haesob park did what iw anted.

this is the program i wrote, it is an encrypter/decrypter for the
robbers language(r�varspr�ket in swedish), its from the book Kalle
Blomkvist by late and very famous author Astrid Lindgren.
you add o + the consonant, nothing is done to vowels.
so d becomes dod, a is just a.

dad is therefore dodadod, super is sosupoperor etc.

puts "Enter sentence to encrypt: "
str = gets

enc = ""
for x in (0..str.length()-1)
  if str.chr =~ /[qwrtpsdfghjklzxcvbnm]/
      enc = enc + str.chr + "o" + str.chr
  else
      enc = enc +str.chr
  end
end

print enc

puts "Enter code to decrypt: "
code = gets

x = 0
dec = ""
while (x<code.length())
  dec = dec + code.chr
  if code.chr =~ /[qwrtpsdfghjklzxcvbnm]/
    x = x + 2
  end
  x = x + 1
end

print dec

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as "a"?

You can do with like this:
if str.chr !~ /[\saeiouy]/

BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")

Regards,
Park Heesob

···

--
Posted via http://www.ruby-forum.com/\.

Not quite. [^\saeiouy] is not the same as the set of consonants.

Todd

···

On Thu, May 8, 2008 at 8:12 AM, Heesob Park <phasis@gmail.com> wrote:

Hi,

globalrev wrote:

ty all for the help. haesob park did what iw anted.

this is the program i wrote, it is an encrypter/decrypter for the
robbers language(r�varspr�ket in swedish), its from the book Kalle
Blomkvist by late and very famous author Astrid Lindgren.
you add o + the consonant, nothing is done to vowels.
so d becomes dod, a is just a.

dad is therefore dodadod, super is sosupoperor etc.

puts "Enter sentence to encrypt: "
str = gets

enc = ""
for x in (0..str.length()-1)
  if str.chr =~ /[qwrtpsdfghjklzxcvbnm]/
      enc = enc + str.chr + "o" + str.chr
  else
      enc = enc +str.chr
  end
end

print enc

puts "Enter code to decrypt: "
code = gets

x = 0
dec = ""
while (x<code.length())
  dec = dec + code.chr
  if code.chr =~ /[qwrtpsdfghjklzxcvbnm]/
    x = x + 2
  end
  x = x + 1
end

print dec

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as "a"?

You can do with like this:
if str.chr !~ /[\saeiouy]/

BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")

that doesnt seem to work, it repeats the sma ething alot of times
befre next letter.

if this is what u meant(why did u add the "s" ?):
for x in (0..str.length()-1)
   str = str.gsub(/([^\aeiouy])/,"\\1o\\1")
end
print str

···

On 8 Maj, 15:12, Heesob Park <pha...@gmail.com> wrote:

Hi,

globalrev wrote:
> ty all for the help. haesob park did what iw anted.

> this is the program i wrote, it is an encrypter/decrypter for the
> robbers language(r�varspr�ket in swedish), its from the book Kalle
> Blomkvist by late and very famous author Astrid Lindgren.
> you add o + the consonant, nothing is done to vowels.
> so d becomes dod, a is just a.

> dad is therefore dodadod, super is sosupoperor etc.

> puts "Enter sentence to encrypt: "
> str = gets

> enc = ""
> for x in (0..str.length()-1)
> if str.chr =~ /[qwrtpsdfghjklzxcvbnm]/
> enc = enc + str.chr + "o" + str.chr
> else
> enc = enc +str.chr
> end
> end

> print enc

> puts "Enter code to decrypt: "
> code = gets

> x = 0
> dec = ""
> while (x<code.length())
> dec = dec + code.chr
> if code.chr =~ /[qwrtpsdfghjklzxcvbnm]/
> x = x + 2
> end
> x = x + 1
> end

> print dec

> i then changed to !~ just like haesob said but if i do /aeiouy / it
> doesnt react to space " ". how do i get it to treat " " as "a"?

You can do with like this:
if str.chr !~ /[\saeiouy]/

BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")

Regards,
Park Heesob
--
Posted viahttp://www.ruby-forum.com/.

> globalrev wrote:
> > i then changed to !~ just like haesob said but if i do /aeiouy / it
> > doesnt react to space " ". how do i get it to treat " " as "a"?
>

It's all in the book...

From "Programming Ruby", in Standard Types/ Regular Expressions
/Character Classes:
" A character class is a set of characters between brackets: ...
[aeiou] will match a vowel ... addition, you can use the abbreviations
... so that (for example) \s matches any whitespace character"
and a bit further down:
"Put a ^ immediately after the opening bracket to negate a character
class: [^a-z] matches any character that isn't a lowercase alphabetic"

That should give you all the information to build the expression you need:
c !~ [\saeiouy]
c
!~ #does not match
/[ #any
\s #whitespace
aeiouy] #or vowel ...

which is exactly what Park Heesob said:

> You can do with like this:
> if str.chr !~ /[\saeiouy]/

An equivalent version moves the 'not' inside the character class:
if str.chr =~ /[^\saeiouy]/

> BTW, your encryption equals to
> str.gsub(/([^\saeiouy])/,"\\1o\\1")

that doesnt seem to work, it repeats the sma ething alot of times
befre next letter.

if this is what u meant (why did u add the "s" ?):

for x in (0..str.length()-1)
  str = str.gsub(/([^\aeiouy])/,"\\1o\\1")
end
print str

That's not what he meant, check the docs for gsub:
"Returns a copy of str with *all* occurrences of pattern replaced with
either replacement or the value of the block". So essentially, gsub
does the loop over all the characters for you. Your whole encode
function can be as simple as:

def encode str; str.gsub(/([^\saeiouy])/,"\\1o\\1"); end

I'd modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode "Hi Oliver" into
"Hohi Ololivoveror", instead of
"HoHi OoOlolivoveror"

HTH,
-Adam

···

On 5/8/08, globalrev <skanemupp@yahoo.se> wrote:

On 8 Maj, 15:12, Heesob Park <pha...@gmail.com> wrote:

I'll repeat. With /[^aeiouy]/ you will get a true for _any_ character
with byte number between 0 and 255 that is not [<whitespace>aeiouy],
including punctuation marks, the capital vowels, and other characters
ala..

"Hello, \n\rtherE!" would become "HoHelollolo,o,\no\n\ro\rtothoherorEoE!o!"

...and also includes everything above 126 and below 32 which is fine
if that's what you want. Of course, any key would work, it doesn't
have to be just consonants, so I guess the point is sort of moot; just
thought I would mention it.

Todd

···

On Thu, May 8, 2008 at 11:10 AM, globalrev <skanemupp@yahoo.se> wrote:

On 8 Maj, 15:12, Heesob Park <pha...@gmail.com> wrote:

Hi,

globalrev wrote:
> ty all for the help. haesob park did what iw anted.

> this is the program i wrote, it is an encrypter/decrypter for the
> robbers language(r�varspr�ket in swedish), its from the book Kalle
> Blomkvist by late and very famous author Astrid Lindgren.
> you add o + the consonant, nothing is done to vowels.
> so d becomes dod, a is just a.

> dad is therefore dodadod, super is sosupoperor etc.

> puts "Enter sentence to encrypt: "
> str = gets

> enc = ""
> for x in (0..str.length()-1)
> if str.chr =~ /[qwrtpsdfghjklzxcvbnm]/
> enc = enc + str.chr + "o" + str.chr
> else
> enc = enc +str.chr
> end
> end

> print enc

> puts "Enter code to decrypt: "
> code = gets

> x = 0
> dec = ""
> while (x<code.length())
> dec = dec + code.chr
> if code.chr =~ /[qwrtpsdfghjklzxcvbnm]/
> x = x + 2
> end
> x = x + 1
> end

> print dec

> i then changed to !~ just like haesob said but if i do /aeiouy / it
> doesnt react to space " ". how do i get it to treat " " as "a"?

You can do with like this:
if str.chr !~ /[\saeiouy]/

BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")

Regards,
Park Heesob
--
Posted viahttp://www.ruby-forum.com/.

that doesnt seem to work, it repeats the sma ething alot of times
befre next letter.

if this is what u meant(why did u add the "s" ?):
for x in (0..str.length()-1)
  str = str.gsub(/([^\saeiouy])/,"\\1o\\1")
end
print str

is there some equally clever way to replace decode?

puts "Enter code to decrypt: "
code = gets
x = 0
dec = ""
while (x<code.length())
        dec = dec + code.chr
        if code.chr !~ /[\saeiouy]/
                x = x + 2
        end
        x = x + 1
end
print dec

···

On 8 Maj, 21:26, Adam Shelly <adam.she...@gmail.com> wrote:

On 5/8/08, globalrev <skanem...@yahoo.se> wrote:> On 8 Maj, 15:12, Heesob Park <pha...@gmail.com> wrote:
> > globalrev wrote:
> > > i then changed to !~ just like haesob said but if i do /aeiouy / it
> > > doesnt react to space " ". how do i get it to treat " " as "a"?

It's all in the book...

From "Programming Ruby", in Standard Types/ Regular Expressions
/Character Classes:
" A character class is a set of characters between brackets: ...
[aeiou] will match a vowel ... addition, you can use the abbreviations
.. so that (for example) \s matches any whitespace character"
and a bit further down:
"Put a ^ immediately after the opening bracket to negate a character
class: [^a-z] matches any character that isn't a lowercase alphabetic"

That should give you all the information to build the expression you need:
c !~ [\saeiouy]
c
!~ #does not match
/[ #any
\s #whitespace
aeiouy] #or vowel ...

which is exactly what Park Heesob said:

> > You can do with like this:
> > if str.chr !~ /[\saeiouy]/

An equivalent version moves the 'not' inside the character class:
if str.chr =~ /[^\saeiouy]/

> > BTW, your encryption equals to
> > str.gsub(/([^\saeiouy])/,"\\1o\\1")

> that doesnt seem to work, it repeats the sma ething alot of times
> befre next letter.

> if this is what u meant (why did u add the "s" ?):

> for x in (0..str.length()-1)
> str = str.gsub(/([^\aeiouy])/,"\\1o\\1")
> end
> print str

That's not what he meant, check the docs for gsub:
"Returns a copy of str with *all* occurrences of pattern replaced with
either replacement or the value of the block". So essentially, gsub
does the loop over all the characters for you. Your whole encode
function can be as simple as:

def encode str; str.gsub(/([^\saeiouy])/,"\\1o\\1"); end

I'd modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode "Hi Oliver" into
"Hohi Ololivoveror", instead of
"HoHi OoOlolivoveror"

HTH,
-Adam

Hi,

globalrev wrote:

···

On 8 Maj, 21:26, Adam Shelly <adam.she...@gmail.com> wrote:

[aeiou] will match a vowel ... addition, you can use the abbreviations
\s #whitespace

> str = str.gsub(/([^\aeiouy])/,"\\1o\\1")

I'd modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode "Hi Oliver" into
"Hohi Ololivoveror", instead of
"HoHi OoOlolivoveror"

HTH,
-Adam

is there some equally clever way to replace decode?

puts "Enter code to decrypt: "
code = gets
x = 0
dec = ""
while (x<code.length())
        dec = dec + code.chr
        if code.chr !~ /[\saeiouy]/
                x = x + 2
        end
        x = x + 1
end
print dec

Something like this:
code.gsub(/([^\saeiouy])o([^\saeiouy])/){$1==$2 ? $1 : $& }

Regards,

Park Heesob
--
Posted via http://www.ruby-forum.com/\.