2

OS: AIX

Shell: KSH

Following the accepted answer on this question I have created an multimensional array. Only, I get an error while trying to print the content of the array.

Error:

Argument "content of $pvid" isn't numeric in array element at...

The script:

#!/usr/bin/perl
use warnings;
use strict;
use Term::ANSIColor;
my @arrpvid = ();

print colored( sprintf("%-10s %9s %8s %8s %8s", 'PVID', 'AIX', 'VIO', 'VTD', 'VHOST'), 'green' ), "\n";
foreach my $pvid (`lspv | awk '{print \$2'}`) {
        foreach my $hdaix (`lspv | awk '{print \$1'}`) {
                chomp $pvid;
                chomp $hdaix;
                push @{ $arrpvid[$pvid] }, $hdaix;
        }
}

print $arrpvid[0][0];

Some explanation:

Basically I want to print 5 variables of 5 different arrays next to each other. The code is written only for 2 arrays.

The content of $pvid:

00088da343b00d9b
00088da38100f93c

The content of $hdaix:

hdisk0
hdisk1
0

2 Answers 2

2

Quick Fix

Looks like you want to use a hash rather than an array, making your inner push

push @{ $arrpvid{$pvid} }, $hdaix;

Note the change from square brackets to curly braces immediately surrounding $pvid. This tells the compiler that you want %arrpvid and not @arrpvid, so be sure to tweak your my declaration as well.

At the end to print the contents of %arrpvid, use

foreach my $pvid (sort { hex $a <=> hex $b } keys %arrpvid) {
  local $" = "][";  # handy trick due to mjd
  print "$pvid: [@{$arrpvid{$pvid}}]\n";
}

The Data::Dumper module is quick and easy output tool.

use Data::Dumper;

$Data::Dumper::Indent = $Data::Dumper::Terse = 1;
print Dumper \%arrpvid;

More Details

You might be tempted to obtain the numeric value corresponding to each hexadecimal string in $pvid with hex as in

push @{ $arrpvid[hex $pvid] }, ...

but given the large example values in your question, @arrpvid would become enormous. Use a hash to create a sparse array instead.

Be sure that all the values of $pvid have the same padding. Otherwise, like values may not hash together appropriately. If you need to normalize, use code along the lines of

$pvid = sprintf "%016x", hex $pvid;
Sign up to request clarification or add additional context in comments.

Comments

1

The problem lies in:

push @{ $arrpvid[$pvid] }, $hdaix;

The $pvid should be a numeric value like 0 or 5 and not i.e. 00088da343b00d9b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.