in reply to Trying to make the code more clear and clean

If you take a look closely, you'll see duplicates of the same group name in the same test.
How to solve it? 

Because you keep pushing them in without checking if that group exists already:

push @{ $href->{test} }, { group => $group, values => [ sort uniq $value, @{ $href->{values} // [] } ] };

Also, this keeps sorting every time you insert a new value:

$a{"values"} = [sort(uniq($value,@{$href->{"values"}}))];

uniq() can perhaps be eliminated if you used a hash instead of the array. And you can do the sorting *once*, after you have read/inserted your data, as a second stage processing.

Replies are listed 'Best First'.
Re^2: Trying to make the code more clear and clean
by ovedpo15 (Pilgrim) on Jul 29, 2019 at 23:01 UTC
    Thank you for the fast reply. I wanted to use hash at first so the key is group and the value is an array of values. But the problem is that this format should be of JSON that will be sent to Mongo and Mongo does not allow dots and the dollar sign to be in the key name. But we want to allow those special chars to be in groups so we had to rechange the structure and they can't be keys. I can't see any other structure than the one I suggested in the post.

    Anyway, I know that I keep pushing it each time, my question here is how I can make sure it gets to the right place? I'll have to iterate over the array and that does not feel efficient. Maybe you can suggest how the structure should look like? Thanks again!