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

On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok [email protected] 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”)?

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.

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?

On Mon, Apr 30, 2012 at 8:33 AM, Ok Ok [email protected] 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/.

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]

Hi,

Use Enumerable#find_all (1) method:

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

On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok [email protected] wrote:


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: [email protected]

*Origin: Happy Hacking!

On Mon, Apr 30, 2012 at 8:33 AM, Ok Ok [email protected] wrote:

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

“abcdefghijkl”.split(/(…)./).reject{|x| x.empty? }.join
=> “abdeghjk”

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

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

‘123123123123’.scan(/(\w\w)\w/).to_s
=> “12121212”

Chris M. 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”

On Mon, Apr 30, 2012 at 5:33 PM, Ok Ok [email protected] 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?

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 12:11 PM, Brian C.
[email protected]wrote:

Chris M. 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:

Good point.

On Mon, Apr 30, 2012 at 7:11 PM, Brian C. [email protected]
wrote:

I think this is right:

a.gsub(/(…)./m) { $1 }
=> “th qic bow fx ums ve te az dg”

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

Robert K. 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”

Why the regex?

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

– Matma R.

On Tue, May 1, 2012 at 5:02 PM, Bartosz Dziewoński [email protected]
wrote:

Why the regex?

s = “abcdefghijklmnopqrstuvwxyz”
s.split(‘’).select.with_index{|l, i| i%3 != 0}.join(‘’)
=> “bcefhiklnoqrtuwxz”

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

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

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

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 Tue, May 1, 2012 at 9:44 AM, Brian C. [email protected]
wrote:

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”

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