Question

I have a large one-dimensional hash with lots of data that I need to structure in such a way that I can sort it easily into a format that is the same each time the code executes.

Original Hash Data:

{
'datetime' => 'datetime value',
'param_name' => 'param name',
'param_value' => 'param value',
'category' => 'category name'
}

Current Data Structure:

{
  'datetime value' => {
    'category' => {
        'param_name' = > 'param name',
        'param_value' => 'param value
    }
  }
}

I can almost build this structure in code, except for every category, there could be multiple param_names and param_values with the same key name.

The problem I have is that if there are multiple param names/values, only the last pair are saved in the new data structure.

I know that keys have to be unique, so I'm not quite sure how to resolve this as of yet.

Once the structure is built, I then need to understand how to sort the data based on datetime, then param_name so that the order is always the same in the output.

Was it helpful?

Solution

Looking at the difference between your first and second example, I think you have your structure a bit off. I think this matches more of what you want:

{
     DATE       => date_time_value,
     PARAMETERS =>  {
                       param_name1 => parameter_value1,
                       param_name2 => parameter_value2
                    }
}

This way, the structure with data may look like this:

 {
      DATE_TIME  => "10/31/2031 12:00am",
      PARAMETERS => {
                       COLOR     => "red",
                       SIZE      => "Really big",
                       NAME      => "Herman",
                    }
 }

Usually, you think of objects having fields which contain values. Think of a row of a SQL table or a spreadsheet. You have columns with headings, and rows that contain the value.

Let's take an employee. They have a name, age, job, and a phone number:

{
      NAME  => "Bob Smith",
      AGE   => "None of your business",
      JOB   => "Making your life miserable",
      PHONE => "555-1212"
}

Unlike a table, each entry could contain other structure. For example, people usually have more than one phone number, and we might want to store the last name separate from the first name:

{
     NAME  => {
                   FIRST => "Bob",
                   LAST  => "Smith"
              }
     AGE   => "None of your business",
     JOB   => "Making your life miserable"
     PHONE =>  {
                   CELL => "555.1234",
                   WORK => "555.1212"
               }
}

Then we have the people who have multiple phones of the same time. For example, Bob has two cell phones. In this case, we'll make each phone type field an array of values:

{
     NAME  => {
                   FIRST => "Bob",
                   LAST  => "Smith",
              }
     AGE   => "None of your business",
     JOB   => "Making your life miserable"
     PHONE =>  {
                   CELL => ["555.1234", "555.4321"]
                   WORK => ["555.1212"]
               }
}

And to initialize it:

my $person = {};
$person->{NAME}->{FIRST} = "Bob";
$person->{NAME}->{LAST}  = "Smith";
$person->{AGE} = "None of your business";
$person->{JOB} = "Making your life miserable";
$person->{PHONE}->{CELL}->[0] = "555.1234";
$person->{PHONE}->{CELL}->[1] = "555.4321";
$person->{PHONE}->{WORK}->[0] = ""555.1212";

OTHER TIPS

I think it seems appropriate to have a params hash where the keys are all of the names and the values are the actual values. It seems like that is what you want.

my %hash = {
  'datetime value' => {
    'category' => {
        'params' => {
            'param-name1' => 'param-value1',
            'param-name2' => 'param-value2',
            'param-name3' => 'param-value3',
            etc..
      }
    }
  }
}

After this restructuring it should be pretty easy to sort based on whatever you would like.

alphabetically by key:

my @alphabetic_keys = sort {  $hash{$a} cmp $hash{$b} } keys %{ $hash{params} };

length by key:

my @by_length_keys = sort { length($a) <=> length($b) } keys %{ $hash{params} };

Assuming category names are unique, I would suggest the following data structure:

{
  'datetime value 1' => {
    'category name 1' => {
        'param name 1' = > [param value1, param value2, ...],
        'param name 2' = > [param value3, param value4, ...],
        etc...
    },
    'category name 2' => {
        'param...' => [ value... ]
  },
  'datetime value 2' => {
    etc...
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top