delete char

Discussion in 'Ruby' started by Pen Ttt, Aug 29, 2010.

  1. Pen Ttt

    Pen Ttt Guest

    i want to make 'gkhj*&()' into 'gkhj',here is my code
    item='gkhj*&()'
    item=item.map{|echar|
    if /[a-z]/=~ echar then
    echar=echar
    else
    echar=''
    end}
    puts item
    why i can't get what i want?
    Any advice appriciated.
    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Aug 29, 2010
    #1
    1. Advertising

  2. Pen Ttt

    Ammar Ali Guest

    On Sun, Aug 29, 2010 at 12:02 PM, Pen Ttt <> wrote:
    > i want to make 'gkhj*&()' into 'gkhj',here is my code
    > =C2=A0item=3D'gkhj*&()'
    > =C2=A0item=3Ditem.map{|echar|
    > =C2=A0if /[a-z]/=3D~ echar then
    > =C2=A0 =C2=A0echar=3Dechar
    > =C2=A0else
    > =C2=A0 =C2=A0echar=3D''
    > =C2=A0end}
    > =C2=A0puts item
    > why i can't get what i want?
    > Any advice appriciated.


    Much easier to use gsub with a negative character set for this:

    puts item.gsub(/[^a-z]+/, '')

    HTH,
    Ammar
     
    Ammar Ali, Aug 29, 2010
    #2
    1. Advertising

  3. Pen Ttt

    Pen Ttt Guest

    Pen Ttt, Aug 29, 2010
    #3
  4. Pen Ttt

    Pen Ttt Guest

    pt@pt-laptop:~$ irb
    irb(main):001:0> 'gkhj*&()'[0]
    => 103
    irb(main):002:0>

    can i get 'gkhj*&()'[0]=>g??


    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Aug 29, 2010
    #4
  5. On Sun, Aug 29, 2010 at 9:45 PM, Pen Ttt <> wrote:
    > it is ok,but i want to know =A0why my code can't run?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    I think you should take the advice of someone who shows you a Ruby way, but=
    ....

    I looked at your code and modified it a little.
    It seems to work but I'm tired so this is all I can offer now.

    item=3D'gkhj*&()'
    #item =3D '((c)?'
    item=3Ditem.split(//).map{|echar|
    if /[a-z]/=3D~ echar then
    echar=3Dechar
    else
    echar=3D''
    end}
    p item*""

    I would not do it this way but you wanted to know why your code did not wor=
    k.



    Harry
     
    Harry Kakueki, Aug 29, 2010
    #5
  6. On Sun, Aug 29, 2010 at 9:56 PM, Pen Ttt <> wrote:
    > pt@pt-laptop:~$ irb
    > irb(main):001:0> =A0'gkhj*&()'[0]
    > =3D> 103
    > irb(main):002:0>
    >
    > can i get =A0'gkhj*&()'[0]=3D>g??
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    p 'gkhj*&()'[0] #Ruby 1.9
    p 'gkhj*&()'.split(//)[0] #Ruby 1.8 ??


    Harry
     
    Harry Kakueki, Aug 29, 2010
    #6
  7. Pen Ttt

    F. Senault Guest

    Le 29 août 2010 à 14:56, Pen Ttt a écrit :

    > pt@pt-laptop:~$ irb
    > irb(main):001:0> 'gkhj*&()'[0]
    > => 103
    > irb(main):002:0>
    >
    > can i get 'gkhj*&()'[0]=>g??


    Not on every ruby version out there. You'd be better using this :

    >> 'gkhj*&()'[0, 1]

    => "g"
    >> 'gkhj*&()'[0..0]

    => "g"

    (First is start index, count, the other is start index..end index ; both
    work on rubies 1.8 and 1.9 at least.)

    Fred
    --
    Hey God, there's nothing left for me to hide
    I lost my ignorance, security and pride
    I'm all alone in this fucking world you must despise (Nine Inch Nails,
    Hey God, I believed your promises, your promises and lies Terrible Lie)
     
    F. Senault, Aug 29, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    595
  2. wwj
    Replies:
    24
    Views:
    2,573
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    505
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    624
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,151
    Ian Collins
    May 9, 2006
Loading...

Share This Page