Perl: loading data by line

I continue to study in Perl.

In this case, I am trying to load data from a .txt file into an array. My script generates output netstatthat looks like this:

Proto Recv-Q Send-Q Local Address    Foreign Address         State       PID/Program name
tcp     0      0 0.0.0.0:3790        0.0.0.0:*               LISTEN      7550/nginx.conf 
tcp     0      0 127.0.1.1:53        0.0.0.0:*               LISTEN      1271/dnsmasq    
tcp     0      0 127.0.0.1:631       0.0.0.0:*               LISTEN      24202/cupsd 

The next step in the process is to put the data that is loaded from the file into an array, and then into the hash, making it sortable by lines, for example, sorting the output to find out all the information that belongs to a specific port number.

My question is: what is the correct way to load this data into an array and then the hash make it available and sortable for output?

0
source share
1 answer

, AoH ( ). , , sort:

my @records = [
 { Proto => "tcp", 'Recv-Q' => 0, ..., 'Local Address' => "0.0.0.0:3790", ..., State => "Listen", ... },
 { Proto => "tcp", 'Recv-Q' => 0, ..., 'Local Address' => "127.0.1.1:53", ..., State => "Listen", ... },
 { Proto => "tcp", 'Recv-Q' => 0, ..., 'Local Address' => "127.0.0.1:631", ..., State => "Listen", ... },
];

my @records_sorted_by_state = sort { $a->{State} cmp $b->{State} } @records;
0

All Articles