in reply to compare values within a hash

One of the most important things to learn when starting to program is precision in your specification of the problem. You must thoroughly understand what you want to program, because the computer is stupid enough to do exactly what you ask.

Now, to help refine your requirements, we need to ask more questions:

Can you have multiple output items in the same group?

What if the first values are not within 10 but the second ones are, what do we want to do?

The main point of the questions is to make certain you are perfectly clear on what should happen for any inputs.

If you can only have one output line for each group, then a hash is a really good way to organize the data. If you can have more than one output line, then you need to get more creative. A hash can only have one value per key, but that value could be a reference to an array.

For example, the data structure for the initial input could be:

my %data = ( 'group1' => [ [32,48], [31,49], [57,91], [52,89] ], 'group3' => [ [10,19] ], 'group4' => [ [23,77] ], );

In this case, each group (key) has an array as a value, and each of those arrays has references to a 2 item list that has the min and max values.

Although not really difficult, this structure might take a little study to become comfortable.

G. Wade

Replies are listed 'Best First'.
Re^2: compare values within a hash
by rookierabbit (Initiate) on Oct 27, 2008 at 17:40 UTC
    Gotcha! I appreciate everyone's help. These ideas are exactly what I am in need of.

    The computer might be stupid, but I know just enough to convince it to really screw up. I have already accomplished the infinite loop!

    I would like to collapse all overlaps into ONE line. I get what you are asking. If the min values are not within 10 but the max values are, then I would like those lines reduced to one representative.

    Your second question is harder for me, I think. I want at least 10 units of separation between the max of one element and the min of the next. 10 is an arbitrary number, but again, its theory I am looking for, I think.

    I always thought I was pretty bright and an abstract thinker, but you guys are in a completely different league. I have lurked for a long time. Whenever I needed to be humbled, I came here!

      I believe you are getting it. The key is that you need to understand what you want well enough to explain it to this stupid computer. As you get more practice, you will learn which requirements you really need to specify well and which you can slide on.

      Unfortunately, bright and abstract thinking are sort of independent of the way to think about this kind of problem. You almost have to look at it as if the computer is going to exploit anything you haven't specified well. The worst part is that sometimes an underspecified program will appear to work and lull you into a false sense of security.

      The answers you are getting are from people that have been bitten over and over again by times when they thought they knew what they were doing.

      Welcome to the wonderful world of programming!

      G. Wade

      The following two statements about what you are trying to do seem very different to me.

      If the groups are the same, I would like to look at the first numerical value. If they are within 10 of each other, I would like to take the lesser value and then look at the second number. If they are within 10, I would like to take the greater value.
      I want at least 10 units of separation between the max of one element and the min of the next.

      If you have the following:

      group1 10 15 group1 22 50

      Do you want these to be merged because the "separation" between 15 and 22 is less than 10 or not merged because neither 10 and 22 nor 15 and 50 are within 10 of each other?