On 01/04/2011 19:52, Wernher Eksteen wrote:
From the folowing list is a result of the @power array, when run through the foreach loop: Pseudo name=emcpowerd 1 lpfc sdba SP A7 active alive 0 0 1 lpfc sddd SP B7 active alive 0 0 3 lpfc sdfg SP B6 active alive 0 0 3 lpfc sdhj SP A6 active alive 0 0 Pseudo name=emcpowerc 1 lpfc sdbb SP A7 active alive 0 0 1 lpfc sdde SP B7 active alive 0 0 3 lpfc sdfh SP B6 active alive 0 0 3 lpfc sdhk SP A6 active alive 0 0 Pseudo name=emcpoweraz 1 lpfc sdbh SP B7 active alive 0 0 3 lpfc sddk SP B6 active alive 0 0 1 lpfc sde SP A7 active alive 0 0 3 lpfc sdfn SP A6 active alive 0 0 From the list above, how can Perl assign the sd* disks to it's relevant emcpower device, so that the output shows this: emcpowerd sdba sddd sdfg sdhj emcpowerc sdbb sdde sdfh sdhk emcpoweraz sdbh sddk sde sdfn This is how the @power array was obtained: $powermt = 'powermt display dev=all'; @power = `$powermt`; foreach my $i (@power) { if (($i =~ /emcpower*/) || ($i =~ /lpfc*/)) { print $i; } }
Please always 'use strict' and 'use warnings', and consequently declare all of your variables. That way most straightforward problems will be solved my Perl before ever reaching his list. It is better to open a pipe to a child process running your command, rather than read all the output into an array and process that. I suggest something like the program below, which builds a hash of the device/disk relationship and prints it out at the end. HTH, Rob use strict; use warnings; my $powermt = 'powermt display dev=all'; open my $fh, '-|', $powermt or die $!; my ($device, %disks); while (my $line = <$fh>) { if ( $line =~ /name=(emcpower\w+)/ ) { $device = $1; } elsif ( $line =~ /\blpfc\s+(sd\w+)/ ) { push @{$disks{$device}}, $1; } } foreach my $device (sort keys %disks) { printf "%s %s\n", $device, join ' ', @{$disks{$device}}; } ** OUTPUT ** emcpoweraz sdbh sddk sde sdfn emcpowerc sdbb sdde sdfh sdhk emcpowerd sdba sddd sdfg sdhj -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/