Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have 5 arrays containing inter-related data that I need to match up. I would like to have a normal hash, but how do I know if I need a hoh or hoa? Regardless, array1 contains diskgroup names, array2 contains filesystem names, array3 contains hdisks, array4 contains sizes of these hdisks and array5 contains lun ids. I would like to have array1 data be the key and the rest of the array's date be values. Or is there a better way? Thank you.
# of elements per array 14 array1 53 array2 416 array3 416 array4 52 array5 I have tried these ways unsuccessfully my %hash = (); @hash{ @array1,@array2,etc } = @list; AND for (@nodupLuns) { $hash{@dgarray}{@fs}{@nodupHdisks}{@sizes} = { 'lunid' => $_ }; } __CODE__ #!/usr/bin/perl use strict; use warnings; $ENV{PATH} = qq(/usr/sbin:/usr/bin); my @dgarray; open (DG, "vxdg list |") or die "vxdg did not open $!"; while (<DG>) { next if /name/i; push @dgarray, +(split)[0]; } close (DG); my (@hdisks,@sizes,@nodupHdisks,@nodupLuns,@lunid,$svc,$dg,$hdisk,@fs, +$lun); for $dg (@dgarray) { open (DF, "df -k |") or die "df did not open $!"; while (<DF>) { if ( /$dg/ ) { push @fs, +(split)[6]; } } close (DF); open (VXP, "vxprint -thvm -g $dg |") or die "vxprint did not open +$!"; while (<VXP>) { if ( /device_tag/ ) { my @tmp = (split /device_tag=/); for $svc (@tmp) { open (VXD, "vxdisk list $svc |") or die "vxdisk list f +ailed $!"; while (<VXD>) { if ( /state=\w+/ ) { push @hdisks, (split /state=enabled/); } } } } } close (VXD); } my %counts; @nodupHdisks = grep { $_ =~ /hdisk\d+/ and ++$counts{$_} < 2 } @hdisks; @sizes = map `bootinfo -s $_`, @nodupHdisks; for $lun (@nodupHdisks) { open (LUNID, "lscfg -vl $lun |") or die "lscfg failed $!"; while (<LUNID>) { if (/serial/i) { push @lunid, substr($_,-33); } } } %counts = (); @nodupLuns = grep { $_ =~ /\w+/ and ++$counts{$_} < 2 } @lunid;

Replies are listed 'Best First'.
Re: arrays, hoh?
by BrowserUk (Patriarch) on Mar 14, 2010 at 08:58 UTC

    Seems to me that you're trying to do an aweful lot more here than just sticking 5 vaguely related arrays of data into a hash under vague key names?

    You're looking to group disk-based information (ie.freespace and bootinfo) for the different drives that constitute a given drive group, together?

    If so, you'll probably need more than just a hash of arrays. Something more like a HoHoHs. And some way to relate what lunids and disk tags and service names related to which disk groups.

    You might get less hasty answers if you posted examples of the output from each of your five commands.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: arrays, hoh?
by ikegami (Patriarch) on Mar 14, 2010 at 02:30 UTC
    Is this what you want?
    my %machine = ( disk_groups => \@disk_groups, file_systems => \@file_systems, hard_disks => \@hard_disks, ... );
Re: arrays, hoh?
by NetWallah (Canon) on Mar 14, 2010 at 05:23 UTC
    UNTESTED rewrite of the first part of your code:
    #!/usr/bin/perl use strict; use warnings; # $ENV{PATH} = qq(/usr/sbin:/usr/bin); ?? This makes no sense - it is +unused my %dg; while (qx|vxdg list|) { next if /name/i; $dg { +(split)[0] } = {}; } #my (@hdisks,@sizes,@nodupHdisks,@nodupLuns,@lunid,$svc,$dg,$hdisk,@fs +,$lun); # Process df -k ------------ for my $dfline ( qx|df -k| ) { $dg{ $_} {FS} = +(split $dfline)[6] if $dfline=~/$_/ for keys %df; } for my $dgname (keys %dg){ for my $vxp ( qx|vxprint -thvm -g $dgname |) { $dg{$dgname}{whatever} = value ... ....
    This way, you build up the structure %df, and don't execute the same connand repeatedly.

    Also , uses qx (backtick equivalent) instead of piped open of command lines.

         Theory is when you know something, but it doesn't work.
        Practice is when something works, but you don't know why it works.
        Programmers combine Theory and Practice: Nothing works and they don't know why.         -Anonymous