Is there a better way to check this array?

I have an array which contains items that need to be renamed. Unfortunately the way things are changed are based on a comparison of if something else exists. For example in this simple array:

names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0']

if my logic was if look at jim, see if fred exists in array, if so, change jim to Jim_Brown.

As I see it now I can't do a simple include? because my array isn't split properly. I've tried doing a grep, but that also doesn't work.

ruby-1.9.2-p290 :020 > names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0']
=> ["sue,5", "tom,6", "jim,7", "sally,8", "fred,9", "barney,0"]
ruby-1.9.2-p290 :021 > names.include?('fred')
=> false
ruby-1.9.2-p290 :022 > names.grep('fred')
=> []

One solution I see is to split the array into a temporary second array and check that split array

ruby-1.9.2-p290 :023 > names.each do | ind |
ruby-1.9.2-p290 :024 > ind_name = ind.split(",")
ruby-1.9.2-p290 :025?> tmparray.push(ind_name[0])
ruby-1.9.2-p290 :026?> end
=> ["sue,5", "tom,6", "jim,7", "sally,8", "fred,9", "barney,0"]
ruby-1.9.2-p290 :027 > puts tmparray
sue
tom
jim
sally
fred
barney
=> nil
ruby-1.9.2-p290 :028 > tmparray.include?('fred')
=> true
ruby-1.9.2-p290 :029 > tmparray.grep('fred')
=> ["fred"]

But there must be a better way that I'm simply overlooking, yes?

Wayne

Wayne Brissette wrote in post #1034881:

As I see it now I can't do a simple include?
ruby-1.9.2-p290 :021 > names.include?('fred')
=> false

How about 'any?', where you can use your own comparison block:

names.any? {|i| i.include?("fred")}

=> true

names.any? {|i| i.include?("frd")}

=> false

···

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

I have an array which contains items that need to be renamed. Unfortunately the way things are changed are based on a comparison of if something else exists. For example in this simple array:

names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0']

if my logic was if look at jim, see if fred exists in array, if so, change jim to Jim_Brown.

As I see it now I can't do a simple include? because my array isn't split properly. I've tried doing a grep, but that also doesn't work.

ruby-1.9.2-p290 :020 > names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0']
=> ["sue,5", "tom,6", "jim,7", "sally,8", "fred,9", "barney,0"]
ruby-1.9.2-p290 :021 > names.include?('fred')
=> false
ruby-1.9.2-p290 :022 > names.grep('fred')
=>

grep should do what you want with a regular expression. Here are few
examples that you can try:

names.grep /fred/

=> ["fred,9"]

names.grep /^fred/

=> ["fred,9"]

names.grep /^fred,\d+/

=> ["fred,9"]

names.grep /^fred,\d+/i

=> ["fred,9"]

names.grep /\Afred,\d+\z/i

=> ["fred,9"]

Regards,
Ammar

···

On Sat, Dec 3, 2011 at 1:06 PM, Wayne Brissette <wbrisett@att.net> wrote:

Thanks. Both of these have given me more food for thought. I knew grep should have worked. The issue Ammar that I see with it though is I have to pass item via a variable and that doesn't seem to be working very well. The any? option Peter suggested does however seem to work. I'll need to see if I notice any performance issues using it though.

Wayne

Thanks. Both of these have given me more food for thought. I knew grep should have worked. The issue Ammar that I see with it though is I have to pass item via a variable and that doesn't seem to be working very well. The any? option Peter suggested does however seem to work. I'll need to see if I notice any performance issues using it though.

FYI, you can use variables inside the regular expression.

name = "fred"

=> "fred"

names.grep /#{name}/i

=> ["fred,9"]

Regards,
Ammar

···

On Sat, Dec 3, 2011 at 2:06 PM, Wayne Brissette <wbrisett@att.net> wrote:

AH! Thanks! I was using it incorrectly. That will teach me to code before I have two cups of coffee. :slight_smile:

Wayne

···

On Dec 3, 2011, at 6:23 AM, Ammar Ali wrote:

On Sat, Dec 3, 2011 at 2:06 PM, Wayne Brissette <wbrisett@att.net> wrote:

Thanks. Both of these have given me more food for thought. I knew grep should have worked. The issue Ammar that I see with it though is I have to pass item via a variable and that doesn't seem to be working very well. The any? option Peter suggested does however seem to work. I'll need to see if I notice any performance issues using it though.

FYI, you can use variables inside the regular expression.

name = "fred"

=> "fred"

names.grep /#{name}/i

=> ["fred,9"]

Why u no use hash?

···

On Sat, Dec 3, 2011 at 7:25 AM, Wayne Brissette <wbrisett@att.net> wrote:

On Dec 3, 2011, at 6:23 AM, Ammar Ali wrote:

> On Sat, Dec 3, 2011 at 2:06 PM, Wayne Brissette <wbrisett@att.net> > wrote:
>> Thanks. Both of these have given me more food for thought. I knew grep
should have worked. The issue Ammar that I see with it though is I have to
pass item via a variable and that doesn't seem to be working very well. The
any? option Peter suggested does however seem to work. I'll need to see if
I notice any performance issues using it though.
>>
>
> FYI, you can use variables inside the regular expression.
>
>>> name = "fred"
> => "fred"
>>> names.grep /#{name}/i
> => ["fred,9"]

AH! Thanks! I was using it incorrectly. That will teach me to code before
I have two cups of coffee. :slight_smile:

Wayne

--
Sincerely,

Isaac Sanders
Section C-4B Vice Chief, Order of the Arrow
Vice Chief of Administration, Tecumseh #65
Eagle Scout

I was wondering the same thing as Isaac. Your data seems quite well
suited to a hash:

irb(main):001:0> names = ['sue,5', 'tom,6', 'jim,7', 'sally,8',
'fred,9', 'barney,0']
=> ["sue,5", "tom,6", "jim,7", "sally,8", "fred,9", "barney,0"]
irb(main):002:0> hashnames = Hash[names.map{|x| x.split ','}]
=> {"sue"=>"5", "tom"=>"6", "jim"=>"7", "sally"=>"8", "fred"=>"9",
"barney"=>"0"}
irb(main):003:0> hashnames.key?('fred')
=> true

Of course I understand you may have other reasons for not using a
hash. But if your name comparisons will always be exact match
comparisons, a hash is ideally suited for quick look-up.

Aaron out.

···

On Sat, Dec 3, 2011 at 5:43 AM, Isaac Sanders <isaacbfsanders@gmail.com> wrote:

Why u no use hash?

I think mostly it's due to my inexperience using Ruby (or any other scripting language that supports hashes). My background in programming/scripting is from days of ole (Fortran, 'c', basic in college) and creating AppleScript code at "the fruit company". I'm really having to brush up on my skills since I'm trying to make my scripts I'm writing now portable outside the Mac OS X platform.

Thank for your example and reminder! It gives me more to think and practice with…

Wayne

···

On Dec 3, 2011, at 10:02 AM, Aaron D. Gifford wrote:

On Sat, Dec 3, 2011 at 5:43 AM, Isaac Sanders <isaacbfsanders@gmail.com> wrote:

Why u no use hash?

I was wondering the same thing as Isaac. Your data seems quite well
suited to a hash: