gpt4 book ai didi

perl - 按降序对第 5 列进行排序错误消息

转载 作者:行者123 更新时间:2023-12-04 18:57:23 27 4
gpt4 key购买 nike

我要排序的文本文件:

MYNETAPP01-NY
700000123456
Filesystem total used avail capacity Mounted on
/vol/vfiler_PROD1_SF_NFS15K01/ 1638GB 735GB 903GB 45% /vol/vfiler_PROD1_SF_NFS15K01/
/vol/vfiler_PROD1_SF_NFS15K01/.snapshot 409GB 105GB 303GB 26% /vol/vfiler_PROD1_SF_NFS15K01/.snapshot
/vol/vfiler_PROD1_SF_isci_15K01/ 2048GB 1653GB 394GB 81% /vol/vfiler_PROD1_SF_isci_15K01/
snap reserve 0TB 0TB 0TB ---% /vol/vfiler_PROD1_SF_isci_15K01/..

我正在尝试按其第 5 列( capacity 字段)按降序对该文本文件进行排序。

当我第一次开始这个时,有一个百分比符号与数字混合。我通过替换值来解决这个问题: s/%/ %/g for @data; 。这使得单独对数字进行排序变得更加容易。之后我会将它改回使用 s/ %/%/g 的方式。

运行脚本后,我收到此错误:

@ACI-CM-L-53:~$ ./netapp.pl
Can't use string ("/vol/vfiler_PROD1_SF_isci_15K01/"...) as an ARRAY ref while "strict refs" in use at ./netapp.pl line 20, line 24 (#1)



   (F) You've told Perl to dereference a string, something which  
use strict blocks to prevent it happening accidentally. See
"Symbolic references" in perlref. This can be triggered by an @ or $
in a double-quoted string immediately before interpolating a variable,
for example in "user @$twitter_id", which says to treat the contents
of $twitter_id as an array reference; use a \ to have a literal @
symbol followed by the contents of $twitter_id: "user \@$twitter_id".

Uncaught exception from user code:  
Can't use string ("/vol/vfiler_PROD1_SF_isci_15K01/"...) as an ARRAY ref while "strict refs" in use at ./netapp.pl line 20, <$DATA> line 24.
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

open (my $DATA, "<raw_info.txt") or die "$!";

my $systemName = <$DATA>;
my $systemSN = <$DATA>;
my $header = <$DATA>;

my @data;
while ( <$DATA> ) {
@data = (<$DATA>);
}

s/%/ %/g for @data;
s/---/000/ for @data;

print @data;

my @sorted = sort { $b->[5] <=> $a->[5] } @data;
print @sorted;

close($DATA);

最佳答案

这是使用 Text::Table 的方法这将很好地将您的输出对齐到整齐的列中。

#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;

open my $DATA, '<', 'file1' or die $!;

<$DATA> for 1 .. 2; # throw away first two lines
chomp(my $hdr = <$DATA>); # header

my $tbl = Text::Table->new( split ' ', $hdr, 6 );

$tbl->load( map [split /\s{2,}/], sort by_percent <$DATA> );
print $tbl;

sub by_percent {
my $keya = $a =~ /(\d+)%/ ? $1 : '0';
my $keyb = $b =~ /(\d+)%/ ? $1 : '0';
$keyb <=> $keya
}

生成的输出是:
Filesystem                              total  used   avail capacity Mounted on                             
/vol/vfiler_PROD1_SF_isci_15K01/ 2048GB 1653GB 394GB 81% /vol/vfiler_PROD1_SF_isci_15K01/
/vol/vfiler_PROD1_SF_NFS15K01/ 1638GB 735GB 903GB 45% /vol/vfiler_PROD1_SF_NFS15K01/
/vol/vfiler_PROD1_SF_NFS15K01/.snapshot 409GB 105GB 303GB 26% /vol/vfiler_PROD1_SF_NFS15K01/.snapshot
snap reserve 0TB 0TB 0TB ---% /vol/vfiler_PROD1_SF_isci_15K01/..

更新

解释程序的一些高级部分。
my $tbl = Text::Table->new( split ' ', $hdr, 6 );

这将创建 Text::Table 对象,其标题分为 6 列。如果没有 6 列的限制,它将创建 7 列(因为最后一个字段“mounted on”也包含一个空格。它会被错误地拆分为 2 列,总共 7 列)。
$tbl->load( map [split /\s{2,}/], sort by_percent <$DATA> );

上面的语句将数据“加载”到表中。 map<$DATA> 中的每一行进行转换.每一行都被分成一个匿名数组,(由 [....] 创建)。分割在 2 个或更多空格上, \s{2,} .如果没有指定,那么带有 1 个空格的数据“快照保留”将被错误地拆分。

我希望这能让事情变得更清楚。

还有一个更简单的示例,它不会像 Text::Table 这样对齐列,而是将它们保留为最初读取的形式:
open my $DATA, '<', 'file1' or die $!;

<$DATA> for 1 .. 2; # throw away first two lines
my $hdr = <$DATA>; # header

print $hdr;
print sort by_percent <$DATA>;

sub by_percent {
my $keya = $a =~ /(\d+)%/ ? $1 : '0';
my $keyb = $b =~ /(\d+)%/ ? $1 : '0';
$keyb <=> $keya
}

关于perl - 按降序对第 5 列进行排序错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com