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