in reply to Proper way to work with columns

Something like this maybe?

use strict; use warnings; my @iostat_system = qx(iostat -x -p -k -d); my @colnames; my $ncols=0; my %disk_data; foreach my $io (@iostat_system) { chomp $io; # print $io."\n"; my @cols=split(' ',$io); next unless ($io); # if ($cols[0]=~/^device/i) { # what if the device is named device? if ($ncols==0 && $cols[0]=~/^device/i) { @colnames=map {$_=~s/[^a-zA-Z0-9]//g;$_} @cols; $ncols=-1+scalar(@colnames); # print join(' ',@colnames)."\n"; } elsif($ncols>0) { my $dev=$cols[0]; for my $i (1..$ncols) { $disk_data{$dev}{$colnames[$i]}=$cols[$i] +;} } } # use Storable; i dont need this !!!! use Data::Dumper; $Data::Dumper::Deepcopy=1; $Data::Dumper::Purity=1; $Data::Dumper::Sortkeys=1; $Data::Dumper::Indent=2; print Dumper(%disk_data);

edit:dropped storable

edit:fix case of device named device

Replies are listed 'Best First'.
Re^2: Proper way to work with columns
by nikita.af (Acolyte) on Dec 28, 2016 at 09:34 UTC

    Hi huch,

    Thanks, it looks like the way I was looking for. I think the line 29:

    for my $i (1..$ncols) { $disk_data{$dev{$colnames[$i]}=$cols[$i] +;}

    should be:

    for my $i (1..$ncols) { $disk_data{$dev{$colnames[$i]}=$cols[$i] +$i;}

    Am I correct?

      Hi nikita.af, no, the line should be

      for my $i (1..$ncols) { $disk_data{$dev}{$colnames[$i]}=$cols[$i]; }
      If you use the download link you will get the real code. The lone + you reference is part of a continuation line indicator provided as a wraparound indicator by the <code>...</code>deliminator

        My bad, thanks a lot for all advises!