如何将简单(平面)数组转换为多维(2D)数组

时间:2014-09-03 12:12:13

标签: arrays perl multidimensional-array

我在Perl语言中有以下代码:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $DF = "/usr/bin/df -Pk"; # HP-UX
my @temp = split /\n/, `$DF`;
shift @temp;
print Dumper \@temp;

输出如下(更短的可读性):

$VAR1 = [
          '/dev/vg00/lvol6       114224    46304    67920    41%     /home',
          '/dev/vg00/lvol7       8340704  4336752  4003952   52%     /opt',
          '/dev/vg00/lvol4       520952    35080   485872     7%     /tmp',
        ];

我想将@temp数组(或创建一个新数组)转换为多维(数组数组),如下所示:

$VAR1 = [
          ['/dev/vg00/lvol6', 114224, 46304, 67920, '41%', '/home'],
          ['/dev/vg00/lvol7', 8340704, 4336752, 4003952, '52%', '/opt'],
          ['/dev/vg00/lvol4', 520952, 35080, 485872, '7%', '/tmp'],
        ];

有人帮忙吗?提前致谢! :)

2 个答案:

答案 0 :(得分:7)

@temp = map [ split ], @temp ;

答案 1 :(得分:1)

我就是这样做的

my @lines = `$DF`;
my @data  = map { [split] } @lines;
print Dumper \@data;

这假设路径中没有意外的空格