The base problem is that I have lots of datapoints with normalized names that are just dumped from the server into a file, but I need to organize these datapoints into a file with rows and columns automatically, according to the data they contain (indicated in their normalized names).
The original file with all the datapoints comes as follows (these are not the original datapoint tags but rather simplified ones):
temp_r301
airflow_r301
temp_r345
airflow_r345
solar_w
solar_e
...
As you can see, they all come as one column, so there is one tag per row.
And I want to organize them so that for each state ("temp" as in temperature), I have the corresponding information in the same row, such as:
temp_r301 301 airflow_r301 solar_w solar_e #airflow in 301 and general solar radiation affect temperature (state) in room 301
temp_r345 345 airflow_r345 solar_w solar_e #airflow in 345 and general solar radiation affect temperature (state) in room 345
Of course the lenght of the array can vary so the idea is to make an algorithm that detects the length and organizes the data accordingly. Also, I am aware I will have to use regular expressions to find the matches and define which datapoints are states and which ones inputs, as well as knowing the room to which they belong.
So far I have tried the following:
use strict;
use warnings;
use diagnostics;
my @transpose = ();
my @sorted = ();
push(@sorted, [qw(temp_r301 temp_r345)]);
push(@sorted, [qw(301 345)]);
push(@sorted, [qw(airflow_r301 airflow_r345 solar_w solar_e)]);
for my $sorted (@sorted) {
for my $column (0 .. $#sorted) {
push(@{$transpose[$column]}, $sorted->[$column]);
}
}
for my $new_row (@transpose) {
for my $new_col (@{$new_row}) {
print "$new_col ";
}
print "\n";
}
But this only works fine if all the arrays have the same lenght (not this case).
I also discovered a loop that can be used to store data into matrix form (array of arrays), but still, I can't seem to find a solution to write in the matrix the data from different arrays:
use strict;
use warnings;
use diagnostics;
use feature 'say';
my @states = qw(temp_r301 temp_r345);
my @zones = qw(301 345);
my @inputs = qw(airflow_r301 airflow_r345 solar_w solar_e);
my @matrix = ();
for my $x (0 .. $#states) {
for my $y (0 .. $#inputs) {
$matrix[$x][$y] = $states[$x]; #of course this only copies the states array and
} #repeats it for each created array
}
for my $aref (@matrix) { #print array of arrays
say "[ @$aref ],";
}
So, knowing that I have all the data dumped into an input file, what would be the best way to sort that data into a matrix? Is there any loop I should give more attention to? Should I be working with arrays?