Delete character from array

Here is my question...

Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]

How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?

Thank You

···

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

If I understand correctly, you want to delete all strings in the array
that are of length 1?
If so you can use delete_if
(Class: Array (Ruby 1.9.2)):

1.9.2p290 :001 > a= ["aa", "bb", "cc", "d", "e"]
=> ["aa", "bb", "cc", "d", "e"]
1.9.2p290 :002 > a.delete_if {|word| word.length == 1}
=> ["aa", "bb", "cc"]
1.9.2p290 :003 > a
=> ["aa", "bb", "cc"]

Jesus.

···

On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok <lists@ruby-forum.com> wrote:

Here is my question...

Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]

How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?

Hi,

Use Enumerable#find_all (1) method:

["aa", "bb", "c", "d", "ee", "f"].find_all{ |c| 1 < c.length }
=> ["aa", "bb", "ee"]

[1]: Module: Enumerable (Ruby 1.9.3)

···

On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok <lists@ruby-forum.com> wrote:

Here is my question...

Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]

How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?

Thank You

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

--
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: zapparov@jabber.ru

*Origin: Happy Hacking!

Yes that was exactly it. I have one more question maybe you can help me
with. Is there a way I could delete every 3rd letter in a string?

···

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

If it was an array you were talking about the index of the letter you
want to delete would be (index + 1) % 3).

In pry or irb enter:

a = (1..20).to_a
a.select{|item| (item + 1) % 3 == 0}

So by that logic:

[8] pry(main)> b = "0123456789".bytes
=> #<Enumerator: ...>
[9] pry(main)> b.select{|item| (item + 1) % 3 == 0}
=> [50, 53, 56]
[10] pry(main)> b.select{|item| (item + 1) % 3 == 0}.each {|c| puts c.chr}
2
5
8
=> [50, 53, 56]

···

On Mon, Apr 30, 2012 at 8:33 AM, Ok Ok <lists@ruby-forum.com> wrote:

Yes that was exactly it. I have one more question maybe you can help me
with. Is there a way I could delete every 3rd letter in a string?

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

--
Doubt is not a pleasant condition, but certainty is absurd.
-- Voltaire

=> "abdeghjk"

P.S. I highly recommend you take the time to *read the docs*,
especially for core elements like String, Array, and so on...

···

On Mon, Apr 30, 2012 at 8:33 AM, Ok Ok <lists@ruby-forum.com> wrote:

Is there a way I could delete every 3rd letter in a string?

"abcdefghijkl".split(/(..)./).reject{|x| x.empty? }.join

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

This is a little blunt, but may be what you need:

'123123123123'.scan(/(\w\w)\w/).to_s
=> "12121212"

···

--
Chris
http://livingsocial.com | http://clabs.org

Another way:

1.9.2p290 :023 > s = "abcdefghi"
=> "abcdefghi"
1.9.2p290 :024 > s.chars.each_slice(3).map {|(a,b,c)| "#{a}#{b}"}.join
=> "abdegh"

Jesus.

···

On Mon, Apr 30, 2012 at 5:33 PM, Ok Ok <lists@ruby-forum.com> wrote:

Yes that was exactly it. I have one more question maybe you can help me
with. Is there a way I could delete every 3rd letter in a string?

Chris Morris wrote in post #1058975:

This is a little blunt, but may be what you need:

'123123123123'.scan(/(\w\w)\w/).to_s
=> "12121212"

That doesn't delete every third character in the string though, as a
couple of simple tests will show:

a = "abcd"

=> "abcd"

a.scan(/(\w\w)\w/).to_s

=> "ab"

a = "the quick brown fox jumps over the lazy dog"

=> "the quick brown fox jumps over the lazy dog"

a.scan(/(\w\w)\w/).to_s

=> "thqubrfojuovthlado"

This is closer:

a.scan(/(..)./m).to_s

=> "th qic bow fx ums ve te az d"

But you can see it's still not right, as it's lost the final "g".

I think this is right:

a.gsub(/(..)./m) { $1 }

=> "th qic bow fx ums ve te az dg"

Here is another way:

a.chars.enum_slice(3).map { |x| x[0,2] }.join

=> "th qic bow fx ums ve te az dg"

···

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

Good point.

···

On Mon, Apr 30, 2012 at 12:11 PM, Brian Candler <lists@ruby-forum.com>wrote:

Chris Morris wrote in post #1058975:
> This is a little blunt, but may be what you need:
>
> '123123123123'.scan(/(\w\w)\w/).to_s
> => "12121212"

That doesn't delete every third character in the string though, as a
couple of simple tests will show:

--
Chris
http://livingsocial.com | http://clabs.org

No need for a block. We can have it a bit more efficient

irb(main):001:0> a = "the quick brown fox jumps over the lazy dog"
=> "the quick brown fox jumps over the lazy dog"
irb(main):002:0> a.gsub /(..)./m, '\\1'
=> "th qic bow fx ums ve te az dg"

But this deletes every third _character_. It will work the same for
OP's strings but may not work in other cases. For really deleting
_letters_ we could do

irb(main):004:0> c=0; a.gsub(/\w/) {|ch| (c+=1) % 3 == 0 ? nil : ch}
=> "th quck ron fx jmp ovr te lzy og"

Kind regards

robert

···

On Mon, Apr 30, 2012 at 7:11 PM, Brian Candler <lists@ruby-forum.com> wrote:

I think this is right:

a.gsub(/(..)./m) { $1 }

=> "th qic bow fx ums ve te az dg"

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1059013:

No need for a block.

I used the block form because I believe it to be clearer (no issues
about backslash escaping)

But this deletes every third _character_. It will work the same for
OP's strings but may not work in other cases. For really deleting
_letters_ we could do

irb(main):004:0> c=0; a.gsub(/\w/) {|ch| (c+=1) % 3 == 0 ? nil : ch}
=> "th quck ron fx jmp ovr te lzy og"

Ah, but aren't all those repeated method calls for integer addition and
modulo arithmetic going to be inefficient? :slight_smile:

You could use a single gsub instead:

a.gsub(/(\w[^\w]*\w[^\w]*)\w([^\w]*)/, '\\1\\2')

=> "th quck ron fx jmp ovr te lzy og"

···

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

Why the regex?

s = "abcdefghijklmnopqrstuvwxyz"
s.split('').select.with_index{|l, i| i%3 != 0}.join('')
=> "bcefhiklnoqrtuwxz"

-- Matma Rex

Oh, yes of course: much better! And we can get rid of all those
brackets and a bit more:

irb(main):001:0> a = "the quick brown fox jumps over the lazy dog"
=> "the quick brown fox jumps over the lazy dog"
irb(main):002:0> a.gsub /(\w\W*\w\W*)\w/, '\\1'
=> "th quck ron fx jmp ovr te lzy og"

Kind regards

robert

···

On Tue, May 1, 2012 at 9:44 AM, Brian Candler <lists@ruby-forum.com> wrote:

Robert Klemme wrote in post #1059013:

No need for a block.

I used the block form because I believe it to be clearer (no issues
about backslash escaping)

But this deletes every third _character_. It will work the same for
OP's strings but may not work in other cases. For really deleting
_letters_ we could do

irb(main):004:0> c=0; a.gsub(/\w/) {|ch| (c+=1) % 3 == 0 ? nil : ch}
=> "th quck ron fx jmp ovr te lzy og"

Ah, but aren't all those repeated method calls for integer addition and
modulo arithmetic going to be inefficient? :slight_smile:

You could use a single gsub instead:

a.gsub(/(\w[^\w]*\w[^\w]*)\w([^\w]*)/, '\\1\\2')

=> "th quck ron fx jmp ovr te lzy og"

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

i dont see the first letter. maybe (i+1)%3 != 0...
kind regards -botp

···

On Tue, May 1, 2012 at 5:02 PM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

Why the regex?

s = "abcdefghijklmnopqrstuvwxyz"
s.split('').select.with_index{|l, i| i%3 != 0}.join('')
=> "bcefhiklnoqrtuwxz"

another variant,

s=("a".."z").to_a.join

=> "abcdefghijklmnopqrstuvwxyz"

"".tap{|o| s.each_char.with_index{|c,i| o << c if (i+1)%3 != 0 }}

=> "abdeghjkmnpqstvwyz"

kind regards -botp

If you want to simply remove every third character in the string s:

0.step(s.size, 3).map {|n| s[n, 2]}.join

···

On 05/01/2012 12:36 PM, botp wrote:

another variant,

s=("a".."z").to_a.join

=> "abcdefghijklmnopqrstuvwxyz"

"".tap{|o| s.each_char.with_index{|c,i| o << c if (i+1)%3 != 0 }}

=> "abdeghjkmnpqstvwyz" kind regards -botp

--
Lars Haugseth