PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > gsub mass substitution
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
gsub mass substitution

Réponse
 
LinkBack Outils de la discussion
Vieux 09/01/2008, 01h44   #1
Ckvok Kovsky
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut gsub mass substitution

Hello. I'm a ruby newbie.
I'd like to make a "mass substitution" version of gsub, like in the sed
example:

$echo "ásturãkolsé" | sed -e 'y/áãé/aae/'
asturakolse

Alike, I'd do something like "ãolélü".gsubm("ãéü","aeu") which would
return "aolelu"

----->Code-1:
class String
def gsubm(letras_ruins,substituir_por_este_char)
resultado = self.split(//).each {|letra_do_self|
letras_ruins.split(//).each {|letra_a_substituir|
letra_do_self.gsub(letra_a_substituir.to_.join.squ eeze,substituir_por_este_char)
#.join.squeeze
#p 'ls:' + letra_do_self.squeeze
}
}
return resultado.join
end
end

p "Leeinád".gsubm("á",'a')a

----->Code-2:
p "leeináãd".sub(/[áã]/,'a')

I didn't get success in any case. I've searched a lot also, but couldn't
find any solution.
Thanks in advance for any .
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/01/2008, 09h31   #2
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

On Jan 9, 2008 2:44 AM, Ckvok Kovsky <mr.oliveira@gmail.com> wrote:
> Hello. I'm a ruby newbie.
> I'd like to make a "mass substitution" version of gsub, like in the sed
> example:
>
> $echo "=E1stur=E3kols=E9" | sed -e 'y/=E1=E3=E9/aae/'
> asturakolse
>
> Alike, I'd do something like "=E3ol=E9l=FC".gsubm("=E3=E9=FC","aeu") whic=

h would
> return "aolelu"


> p "Leein=E1d".gsubm("=E1",'a')a
>
> I didn't get success in any case. I've searched a lot also, but couldn't
> find any solution.


Maybe tr will work for you:

irb(main):012:0> "abcdefghijk".tr "abc", "123"
=3D> "123defghijk"

It supports character ranges, and padding the second argument
with its last character if it's shorter than the first one:

"hello".tr('aeiou', '*') #=3D> "h*ll*"
"hello".tr('^aeiou', '*') #=3D> "*e**o"
"hello".tr('el', 'ip') #=3D> "hippo"
"hello".tr('a-y', 'b-z') #=3D> "ifmmp"

I was having some problems testing your cases, I think because of the
strange chars, maybe that's the source of your problems too?

Hope this s,

Jesus.

  Réponse avec citation
Vieux 09/01/2008, 10h39   #3
Ckvok Kovsky
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

Jesús Gabriel y Galán wrote:
> On Jan 9, 2008 2:44 AM, Ckvok Kovsky <mr.oliveira@gmail.com> wrote:
>> Hello. I'm a ruby newbie.
>> I'd like to make a "mass substitution" version of gsub, like in the sed
>> example:
>>
>> $echo "�stur�kols�" | sed -e 'y/���/aae/'
>> asturakolse
>>
>> Alike, I'd do something like "�ol�l�".gsubm("���","aeu") which would
>> return "aolelu"

>
>> p "Leein�d".gsubm("�",'a')a
>>
>> I didn't get success in any case. I've searched a lot also, but couldn't
>> find any solution.

>
> Maybe tr will work for you:
>
> irb(main):012:0> "abcdefghijk".tr "abc", "123"
> => "123defghijk"
>
> It supports character ranges, and padding the second argument
> with its last character if it's shorter than the first one:
>
> "hello".tr('aeiou', '*') #=> "h*ll*"
> "hello".tr('^aeiou', '*') #=> "*e**o"
> "hello".tr('el', 'ip') #=> "hippo"
> "hello".tr('a-y', 'b-z') #=> "ifmmp"
>
> I was having some problems testing your cases, I think because of the
> strange chars, maybe that's the source of your problems too?
>
> Hope this s,
>
> Jesus.


The "strange chars" are common chars in Brazilian Portuguese and
Portuguese as well as other languages. e.g.: francês, inglês, pátria,
nação, aeroviário, silábica, and so on.

Ruby wasn't designed to deal with those chars in the same way you can
deal with english-only chars.

"som".tr('sm','NM') #=> "NoM"
"cruxificação".tr("çã","ca") #=> "cruxificaaaaao"

$ ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]

Maybe it's better to use a system() call and execute sed or start
learning some other language like perl.

Thanks once again.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/01/2008, 11h28   #4
Daniel Brumbaugh Keeney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

On Jan 9, 2008 4:39 AM, Ckvok Kovsky wrote:
> Ruby wasn't designed to deal with those chars in the same way you can
> deal with english-only chars.


Have you tried setting the character set to Unicode? That can make a
difference. Also, before you abandon Ruby altogether, be aware that
Ruby 1.9 deals with characters completely differently than Ruby 1.8,
so those problems you're having might go away when you upgrade (not
necessarily now, of course, 1.9 being officially 'experimental')


Daniel Brumbaugh Keeney

  Réponse avec citation
Vieux 09/01/2008, 11h42   #5
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

On Jan 9, 2008 12:28 PM, Daniel Brumbaugh Keeney
<devi.webmaster@gmail.com> wrote:
> On Jan 9, 2008 4:39 AM, Ckvok Kovsky wrote:
> > Ruby wasn't designed to deal with those chars in the same way you can
> > deal with english-only chars.

>
> Have you tried setting the character set to Unicode? That can make a
> difference. Also, before you abandon Ruby altogether, be aware that
> Ruby 1.9 deals with characters completely differently than Ruby 1.8,
> so those problems you're having might go away when you upgrade (not
> necessarily now, of course, 1.9 being officially 'experimental')


I've tried this and still doesn't work as expected

jesus@jesus-laptop:~$ irb1.8 -KU
irb(main):001:0> "=E2bcd=EB".tr "=E2=EB", "ae"
=3D> "eebcdee"

Maybe someone can explain if this should work as the OP
and myself are expecting or not and why?

Jesus.

  Réponse avec citation
Vieux 09/01/2008, 14h03   #6
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

On Jan 9, 2008 2:53 PM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: gsub mass substitution"
> on Wed, 9 Jan 2008 20:42:57 +0900, "=3D?ISO-8859-1?Q?Jes=3DFAs_Gabrie=

l_y_Gal=3DE1n?=3D" <jgabrielygalan@gmail.com> writes:
>
> |I've tried this and still doesn't work as expected
> |
> |jesus@jesus-laptop:~$ irb1.8 -KU
> |irb(main):001:0> "=E2bcd=EB".tr "=E2=EB", "ae"
> |=3D> "eebcdee"
>
> Adding
>
> require 'jcode'
>
> may you,


Thanks, Matz. This works:

jesus@jesus-laptop:~$ irb1.8 -KU
irb(main):001:0> require 'jcode'
=3D> true
irb(main):002:0> "=E2bcd=EB".tr "=E2=EB", "ae"
=3D> "abcde"

Although I don't understand all the nuances that are coming into play here.

> or bring you another trouble. YMMV.


This left me a bit nervous, but as I'm not in need of this (I'm not the OP)
I'll leave it for later when I have more time to understand more about
this issue.

Thanks for answering,

Jesus.

  Réponse avec citation
Vieux 09/01/2008, 16h53   #7
Ckvok Kovsky
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub mass substitution

I should thank you three for the solution. It works in the manner Jesús
has posted. Thank you all.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h30.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14470 seconds with 15 queries