Perl的:按行加载数据 [英] Perl: Load data by row

查看:65
本文介绍了Perl的:按行加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继续我的Perl的学习。

I'm continuing my Perl learning.

在这种情况下,我想从txt文件的数据加载到阵列。我的脚本生成的netstat 输出看起来像这样:

In this case I'm trying to load data from .txt file into array. My script generates netstat output which 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 

在进程下一步就是把它从文件加载到数组,然后在散列数据,使它通过排序行,例如排序输出,找出属于特定的端口号的所有信息。

Next step in process is to put data which is loaded from file into array and then in hash, making it sortable by rows, for example sorting output to find out all information which belongs specific port number.

我的问题是:什么是加载该数据到数组有道,然后散列,使之获得和排序为输出

My question is: What is proper way to load this data into array and then hash to make it accessible and sortable for the output?

推荐答案

我想你需要AOH(哈希数组)。之后,你可以得到你想要通过自定义的一切排序

I think you need AoH (array of hashes). After that you can get everything you want with custom 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;

这篇关于Perl的:按行加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆