Forum: Ruby Deleting numbers in array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B720ff06417313ad1fec35b4922dfbab?d=identicon&s=25 Clayton Lane (claytonl)
on 2009-04-27 02:54
I'm new to Ruby and unsure how to delete numbers in an array. I have an
array filled with numbers from 1 to 100. If I use nums.delete_if {|x| x
== 9 }, it will delete 9, but I want to remove any number containing 9.
For example, 19, 39, 92, 99 etc. I tried using a wildcard, but it didn't
work. I'm sure theres an easy command to do it, but I wasn't able to
find it on in Ruby documentation. I also want to remove numbers
containing 0,6,7, and 8 as well. Any help would be greatly appreciated.
Thanks!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-04-27 03:01
(Received via mailing list)
Hi --

Clayton Lane wrote:
> I'm new to Ruby and unsure how to delete numbers in an array. I have an
> array filled with numbers from 1 to 100. If I use nums.delete_if {|x| x
> == 9 }, it will delete 9, but I want to remove any number containing 9.
> For example, 19, 39, 92, 99 etc. I tried using a wildcard, but it didn't
> work. I'm sure theres an easy command to do it, but I wasn't able to
> find it on in Ruby documentation. I also want to remove numbers
> containing 0,6,7, and 8 as well. Any help would be greatly appreciated.

You're pretty much going to have to convert the numbers to strings. For
example:

   array.delete_if {|x| x.to_s[/[06789]/] }


David
B720ff06417313ad1fec35b4922dfbab?d=identicon&s=25 Clayton Lane (claytonl)
on 2009-04-27 03:21
David A. Black wrote:
> Hi --
>
> Clayton Lane wrote:
>> I'm new to Ruby and unsure how to delete numbers in an array. I have an
>> array filled with numbers from 1 to 100. If I use nums.delete_if {|x| x
>> == 9 }, it will delete 9, but I want to remove any number containing 9.
>> For example, 19, 39, 92, 99 etc. I tried using a wildcard, but it didn't
>> work. I'm sure theres an easy command to do it, but I wasn't able to
>> find it on in Ruby documentation. I also want to remove numbers
>> containing 0,6,7, and 8 as well. Any help would be greatly appreciated.
>
> You're pretty much going to have to convert the numbers to strings. For
> example:
>
>    array.delete_if {|x| x.to_s[/[06789]/] }
>
>
> David

Wow! Ruby is so good! I'm unsure of how to do this final step too. If
any numbers don't contain 1, remove them. Any ideas?
6e191124d011c102f295ae1bccf20ee1?d=identicon&s=25 Robert Schaaf (Guest)
on 2009-04-27 03:23
(Received via mailing list)
Hello Clayton,

Try this.

nums.delete_if {|x| x.to_s.include? '9'}

Bob Schaaf
B720ff06417313ad1fec35b4922dfbab?d=identicon&s=25 Clayton Lane (claytonl)
on 2009-04-27 03:36
Robert Schaaf wrote:
> Hello Clayton,
>
> Try this.
>
> nums.delete_if {|x| x.to_s.include? '9'}
>
> Bob Schaaf

Hmmm didn't seem to work. I'm just trying to remove numbers that don't
contain 1. For example, 21 would be allowed, but 22 wouldn't.
6e191124d011c102f295ae1bccf20ee1?d=identicon&s=25 Robert Schaaf (Guest)
on 2009-04-27 03:38
(Received via mailing list)
nums.delete_if {|x| !x.to_s.include? '1'}
B720ff06417313ad1fec35b4922dfbab?d=identicon&s=25 Clayton Lane (claytonl)
on 2009-04-27 03:45
Robert Schaaf wrote:
> nums.delete_if {|x| !x.to_s.include? '1'}

Amazing! I tried to do this in C++ and it was a nightmare. Ruby is
amazing. Thanks for the help!
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 03:52
Clayton Lane wrote:
> David A. Black wrote:
>> Hi --
>>
>> Clayton Lane wrote:
>>> I'm new to Ruby and unsure how to delete numbers in an array. I have an
>>> array filled with numbers from 1 to 100. If I use nums.delete_if {|x| x
>>> == 9 }, it will delete 9, but I want to remove any number containing 9.
>>> For example, 19, 39, 92, 99 etc. I tried using a wildcard, but it didn't
>>> work. I'm sure theres an easy command to do it, but I wasn't able to
>>> find it on in Ruby documentation. I also want to remove numbers
>>> containing 0,6,7, and 8 as well. Any help would be greatly appreciated.
>>
>> You're pretty much going to have to convert the numbers to strings. For
>> example:
>>
>>    array.delete_if {|x| x.to_s[/[06789]/] }
>>
>>
>> David
>
> Wow! Ruby is so good! I'm unsure of how to do this final step too. If
> any numbers don't contain 1, remove them. Any ideas?

arr = [51, 52, 20, 36, 79]
result = arr.delete_if {|x| x.to_s[/[06789]/]}
p result

--output:--
[51, 52]

result = arr.delete_if do |x|
  str = x.to_s
  str[/[02345]/] and not str[/1/]
end
p result

--output:--
[51]
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 05:16
Clayton Lane wrote:
> Robert Schaaf wrote:
>> nums.delete_if {|x| !x.to_s.include? '1'}
>
> Amazing! I tried to do this in C++ and it was a nightmare.

Not too bad:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

bool mytest(int x)
{
  //convert x to string:

  string str;
  stringstream my_ss;
  my_ss<<x;
  my_ss>>str;


  //perform test:

  string bad_numbers = "06789";

  if(str.find_first_of(bad_numbers) != string::npos)
    return true; //found a bad number in str
  else if(str.find("1") == string::npos)
    return true; //found '1' in str
  else
    return false; //don't remove
}

int main()
{
  int arr[] = {51, 52, 20, 36, 79};
  int size = 5;

        //insert numbers in a vector:
  vector<int> numbers(arr, arr+size);

  //remove bad numbers:
  vector<int>::iterator new_end;
  new_end = remove_if(numbers.begin(), numbers.end(), mytest);
  numbers.erase(new_end, numbers.end());

  //display the results:
  vector<int>::iterator pos = numbers.begin();
  for(pos; pos != numbers.end(); ++pos)
  {
    cout<<*pos<<" ";
  }
  cout<<endl;

  return 0;

}

--output:--
51
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 05:35
7stud -- wrote:
> Clayton Lane wrote:
>> David A. Black wrote:
>>> Hi --
>>>
>>> Clayton Lane wrote:
>>>> I'm new to Ruby and unsure how to delete numbers in an array. I have an
>>>> array filled with numbers from 1 to 100. If I use nums.delete_if {|x| x
>>>> == 9 }, it will delete 9, but I want to remove any number containing 9.
>>>> For example, 19, 39, 92, 99 etc. I tried using a wildcard, but it didn't
>>>> work. I'm sure theres an easy command to do it, but I wasn't able to
>>>> find it on in Ruby documentation. I also want to remove numbers
>>>> containing 0,6,7, and 8 as well. Any help would be greatly appreciated.
>>>
>>> You're pretty much going to have to convert the numbers to strings. For
>>> example:
>>>
>>>    array.delete_if {|x| x.to_s[/[06789]/] }
>>>
>>>
>>> David
>>
>> Wow! Ruby is so good! I'm unsure of how to do this final step too. If
>> any numbers don't contain 1, remove them. Any ideas?
>
> arr = [51, 52, 20, 36, 79]
> result = arr.delete_if {|x| x.to_s[/[06789]/]}
> p result
>
> --output:--
> [51, 52]
>
> result = arr.delete_if do |x|
>   str = x.to_s
>   str[/[02345]/] and not str[/1/]
> end
> p result
>
> --output:--
> [51]

lol.  I screwed that up!  I guess it was easier for me in C++ than ruby.
Hmm...for a ruby solution, I find that I have to use the same structure
as the C++ program:

arr = [51, 52, 22, 20, 36, 79]
result = arr.delete_if {|x| x.to_s[/[06789]/]}
p result

result = arr.delete_if do |x|
  str = x.to_s

  if str[/06789/]
    true #delete
  elsif not str[/1/]
    true #delete
  else
    false #keep
  end

end
p result

--output:--
[51, 52, 22]
[51]
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 05:39
7stud -- wrote:
>
>   if(str.find_first_of(bad_numbers) != string::npos)
>     return true; //found a bad number in str
>   else if(str.find("1") == string::npos)
>     return true; //found '1' in str

Bad comment for the last line.  It should be:

>   else if(str.find("1") == string::npos)
>     return true; //did not find a '1' in str
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-04-27 12:20
(Received via mailing list)
Hi --

7stud -- wrote:
>>>>> work. I'm sure theres an easy command to do it, but I wasn't able to
>>> any numbers don't contain 1, remove them. Any ideas?
>> end
> result = arr.delete_if {|x| x.to_s[/[06789]/]}
>      false #keep
>    end
>
> end
> p result
>
> --output:--
> [51, 52, 22]
> [51]

You're working much too hard :-) I'm not sure whether the OP only cares
about the 1's, or wants to do both tests, but assuming the latter:

   arr.delete_if {|x| s = x.to_s; s[/[06789]/] ||! s[/1/] }


David
This topic is locked and can not be replied to.