0

I have a 2-D ruby array which looks something like this :

(Array) - @main.each do |value| 

value[0]    value[1]   value[2]

3.0.1.2     TOTAL      Pass
3.0.1.3     TOTAL      Pass
3.4.0.1     8K         Fail
3.5.4.3     9K         Fail
3.5.0.9     TOTAL      Fail

I want to seperate them into 2 arrays such that first one should have all rows with 'TOTAL' and the other array should have everything else.

3 Answers 3

4
@main = [
  %w[3.0.1.2     TOTAL      Pass],
  %w[3.0.1.3     TOTAL      Pass],
  %w[3.4.0.1     8K         Fail],
  %w[3.5.4.3     9K         Fail],
  %w[3.5.0.9     TOTAL      Fail],
]

totals, others = @main.partition{|a| a[1] == 'TOTAL'}

# totals => [["3.0.1.2", "TOTAL", "Pass"], ["3.0.1.3", "TOTAL", "Pass"], ["3.5.0.9", "TOTAL", "Fail"]]
# others => [["3.4.0.1", "8K", "Fail"], ["3.5.4.3", "9K", "Fail"]]
Sign up to request clarification or add additional context in comments.

3 Comments

This does partition it, but the new arrays have only the a[1] field.....I want the other fields to remain intact too.
@sagarvikani When I run it, the new arrays contain the original tuples. Enumerable#partition partitions based on the block, but produces the original elements.
@sagarvikani Perhaps you could more accurately show us your input, since your question has a rather obscure representation? I've updated this answer to demonstrate exactly how I interpreted your @main to be structured.
1

I don't quite follow what the fields of your array are but you can use select and reject:

totals = @main.select{|entry| entry == "TOTAL"}
others = @main.reject{|entry| entry == "TOTAL"}

For whatever test you have of each element. The original @main will stay as it was and you can use totals and others to access the elements.

1 Comment

1

Your question isn't explained well, but as a quick guess for what you want I'll recommend looking at Array.group_by, which is part of Enumerable:

ary = [
  %w[3.0.1.2     TOTAL      Pass],
  %w[3.0.1.3     TOTAL      Pass],
  %w[3.4.0.1     8K         Fail],
  %w[3.5.4.3     9K         Fail],
  %w[3.5.0.9     TOTAL      Fail],
]


ary.group_by{ |a| a.last }

Which returns a hash, with the keys being the two different values for the last element in the sub-arrays:

{
    "Pass" => [
        [0] [
            [0] "3.0.1.2",
            [1] "TOTAL",
            [2] "Pass"
        ],
        [1] [
            [0] "3.0.1.3",
            [1] "TOTAL",
            [2] "Pass"
        ]
    ],
    "Fail" => [
        [0] [
            [0] "3.4.0.1",
            [1] "8K",
            [2] "Fail"
        ],
        [1] [
            [0] "3.5.4.3",
            [1] "9K",
            [2] "Fail"
        ],
        [2] [
            [0] "3.5.0.9",
            [1] "TOTAL",
            [2] "Fail"
        ]
    ]
}

Accessing one or the other is easy:

ary.group_by{ |a| a.last }['Pass']
[
    [0] [
        [0] "3.0.1.2",
        [1] "TOTAL",
        [2] "Pass"
    ],
    [1] [
        [0] "3.0.1.3",
        [1] "TOTAL",
        [2] "Pass"
    ]
]

Or:

ary.group_by{ |a| a.last }['Fail']
[
    [0] [
        [0] "3.4.0.1",
        [1] "8K",
        [2] "Fail"
    ],
    [1] [
        [0] "3.5.4.3",
        [1] "9K",
        [2] "Fail"
    ],
    [2] [
        [0] "3.5.0.9",
        [1] "TOTAL",
        [2] "Fail"
    ]
]

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.