I have a .txt file with data in one column like so:
state_1
state_2
state_3
input_11
input_12
input_13
input_21
input_22
input_31
And I want to group these tags into a hash according to their names and numbers:
state_1: # For state_1, we have all "input1x" data tags
-input_11
-input_12
-input_13
state_2: # For state_2, we have all "input2x" data tags
-input_21
-input_22
state_3: # For state_3, we have all "input3x" data tags
-input_31
I tried using the push function like in this case to try and force the values into the hashes as arrays with push @{ $state{$inputs} }, $_;, but I am stuck finding a way to loop and get the desired hash according to the regular expressions.
I have also researched how to group my data according to regex, but I still cannot find a way to group the data as I get it from the .txt file.
My question is, what am I looking for to be able to group these tags accordingly?
CodePudding user response:
In short, extract that index (1 or 2...) from a line of data and use it to form the right key and add the line to that key's arraref, also relying on autovivification
push @{$data{"state_$1"}}, $line if $line =~ /input_([0-9])/;
On lines with data input_NN the regex extracts the first number after input_, and adds that line to the arrayref at its suitable key. The key name to add to is built using the captured number and the fixed prefix. The very first time round for a key, before it's ever been seen and so it isn't in the hash yet, it is made by the mechanism/feature called autovivification.†
Then there are a few details -- if what is there in real data, instead of the token names input and state, isn't known ahead of time it can be extracted from the first line of data, and then used as above. The linefeed need be removed as the line is read. Altogether
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd); # to see complex data; or, use core Data::Dumper
my 