0

How would this get done? Assume I have the following

arr = [[test, 0, 0, 0], [apples, 0, 9, 8]]

I know I would do something like:

def delete_me(item)
    arr.each do |a|
        if a[0] == item
            #delete the array containing test
        end
    end
end

delete_me('test')

As far as I can see you can only do: a.remove() but that leaves me with a empty [],m I don't want that, I want it completely gone.

1

4 Answers 4

1

You can use delete_if and match the first term to your argument:

arr = [['test', 0, 0, 0], ['apples', 0, 9, 8]]

def delete_me(array, term)
  array.delete_if {|x, *_| x == term }
end

(I've included the array as an argument as well, as the execution context is not clear from your post).

Sign up to request clarification or add additional context in comments.

Comments

1

Following up on @iamnotmaynard's suggestion:

arr.delete_if { |a| a[0] == 'test' }

Comments

0

assoc.

arr.delete(arr.assoc("test"))

Comments

0

I had a similar need to remove one or more columns that matched a text pattern.

col_to_delete = 'test'
arr = [['test','apples','pears'],[2,3,5],[3,6,8],[1,3,1]]
arr.transpose.collect{|a| a if (a[0] != col_to_delete)}.reject(&:nil?).transpose
=> [["apples", "pears"], [3, 5], [6, 8], [3, 1]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.