Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array or hash output

Status
Not open for further replies.

denisl

Technical User
Jun 18, 2001
36
US
Hi, I built 2 arrays they are:

@virtuals and @physical
Each array will have 2-5 elememts in them.
What I'm trying to do is, for each element in the array create 2 variables. The first variable will be the element and the second variable will be all the other elements in both the @physicals and @virtuals array EXCEPT for the initial element, is CSV format.

For example:
@physicals = p1 p2 p3 p4
@virtuals = v1 v2

I want a to create the following in a loop:

loop1:
var1="p1"
var2="p2,p3,p4,v1,v2"

loop2:
var1="p2"
var2="p1,p3,p4,v1,v2"

loop3:
var1="p3"
var2="p1,p2,p4,v1,v2"

etc.. etc..

What I'm going to end up doing is within the loop after the variables have been set is run a command using the variables.
The next loop will overwirte the variables and I rerun my commands.

The purpose of this script is to define proxynode relationships in Tivoli Storage Manager.
The command I need to run is:
grant proxy target=$var1 agent=$var2

The agent= has to be a csv list.

Here's the script to give you an idea..

Code:
#!/usr/bin/perl
#
#
########################################
########################################
$date = `date "+%m%d%Y"`;
chomp $date ;
$work = "/adsm/SCRIPTS/HOLDBACKUPS/" ;
$dsmpgm = "/usr/tivoli/tsm/client/ba/bin/dsmadmc";


&main_menu;

###################################################################
###### SUB ROUTINES       #########################################
###################################################################


##### sub main menu
sub main_menu {
system "clear";
print "\n";
print "-------------------------------------------------\n";
print "Cluster Node TSM Server Configuration Utility    \n";
print "-------------------------------------------------\n";
print "\n";
print "1\tCreate a new cluster\n";
print "2\tAdd a new physical or virtual node to an existing cluster\n";
print "3\tRemove an existing physical or virtual node from an existing cluster\n";
print "\n";
print "0\tExit \n";
print "\n";
print "Please make a selection: ";

my $clustactivity = <STDIN>;

if ( $clustactivity !~ /[1-3,0]/) {
        print "\n" ;
        print "Invalid Selection" ;
        print "\n" ;
        sleep 2;
        &main_menu;
   } # End if clustactivty

##
## Goto next sub based on selection
##

&newcluster if ($clustactivity =~ 1);
&addcluster if ($clustactivity =~ 2);
&delcluster if ($clustactivity =~ 3);
die "Exiting on user request" if ($clustactivity =~ 0);

}
############ End sub main menu

sub newcluster {

#### PHYSICALS
$number = 1 ;
while ($done ne "stop") {
  
  print "\nPlease enter the number $number physical node in the cluster: ";
  $node = <STDIN>;
  chomp($node);
  push(@physicals,$node);
  $number++; 
  print "\nWould you like to add another physical host (y/n)? ";
  $ans = <STDIN>;
  chomp($ans);

        if ( $ans eq "n" ) {
        $done=stop;
        }
        elsif ( $ans ne "y" ) {
        print "Invalid response. \"y\" or \"n\" only. Please restart!\n";
        exit
        }
}

#### VIRTUALS

$number = 1 ;
while ($vdone ne "stop") {

  print "\nPlease enter the number $number virtual node in the cluster: ";
  $node = <STDIN>;
  chomp($node);
  push(@virtuals,$node);
  $number++;
  print "\nWould you like to add another virtual host (y/n)? ";
  $ans = <STDIN>;
  chomp($ans);

        if ( $ans eq "n" ) {
        $vdone=stop;
        }
        elsif ( $ans ne "y" ) {
        print "Invalid response. \"y\" or \"n\" only. Please restart!\n";
        exit
        }
}

print "\nYou have entered the following nodes:\n\n";

print "Physical Nodes: ";
foreach (@physicals) {
print $_ . "\t";
}
print "\n";
print "Virtual Nodes: ";
foreach (@virtuals) {
print $_ . "\t";
}
print "\n";
print "\n";
print "Is this information correct (y/n)? ";
$ans = <STDIN>;
chomp($ans);
if ( $ans ne "y" ) {
print "\nYour answer was \"$ans\". Exiting script!\n";
exit 
}

print "Please enter your TSM Administrator ID: ";
$tsmadmin = <STDIN>;
chomp($tsmadmin);
print "\nPlease enter your TSM Adminstrator Password: ";
system("stty -echo");
$tsmpa = <STDIN>;
system("stty echo");
chomp($tsmpa);
print "\n";
print "Please select which TSM Server you want to register this cluster on: \n";
print "\n";
print "1.\tTSMPC1\n";
print "2.\tTSMPC2\n";
print "3.\tTSMTGA1\n";
print "4.\tTSMTGA2\n";
print "5.\tADSMTE\n";
print "\n";
print "Selection: ";
$ans = <STDIN>;
chomp($ans);
print "$ans\n" ;

if ( $ans == 1 ) { $tsmserver = tsmpc1;  }
if ( $ans == 2 ) { $tsmserver = tsmpc2;  }
if ( $ans == 3 ) { $tsmserver = tsmtga1; }
if ( $ans == 4 ) { $tsmserver = tsmtga2; }
if ( $ans == 5 ) { $tsmserver = adsmte;  }

if ( $ans !~ /^[1-5]$/ ) {
print "Invalid Selection! Exiting Script.\n" ;
exit
}
print "\n";
print "Verifying connectivity to $tsmserver.....";

system("$dsmpgm -se=$tsmserver -id=$tsmadmin -pa=$tsmpa quit > /dev/null 2>&1");
$retcode=$? ;
if ( $retcode == 0 ) {
print "[SUCCESS]\n";
}
elsif ( $retcode != 0 ) {
print "[FAILED!!]\n";
print "Exiting script\n";
exit
}

## Create proxy agent target for all nodes

foreach (@physicals) {
%targethash="";
$agent=$_;
        foreach (@physicals) {
          $target=$_;
          if ( $target ne $agent ) {
            #push(@targetnodes,$target);
            $targethash{$target} = $target ;
          }
        }
        foreach (@virtuals) {
          $target=$_;
          if ( $target ne $agent ) {
            #push(@targetnodes,$target);
            $targethash{$target} = $target ;
          }
        }
print "\nAgent node $agent: Targets: ";

for $key ( keys %targethash ) {
        $value = $targethash{$key};
        print "$value ";
    }



print (@targetnodes)."\n";
print "\n";
}










} ## end newcluster sub

#sub addcluster {
#}

#sub delcluster {
#}

Thanks!!
 
Proof of Concept (if I'm following you correctly):
Code:
my @physicals = ( 'p1', 'p2', 'p3', 'p4', 'p5');
my @virtuals  = ( 'v1', 'v2' );
my @all       = ( @physicals, @virtuals );
my $list      = ','.join(',', @all);

foreach (@all) {
	my ($var1, $var2) = ( $_, $list );
  $var2 =~ s/,$_//;
  $var2 = substr( $var2, 1);
  print "Var1 = $var1 *** Var2 = $var2 \n";
}


 
Another way which avoids problems if there can be duplicate values in the arrays:

Code:
my @physicals = ( 'p1', 'p2', 'p3', 'p4', 'p5');
my @virtuals  = ( 'v1', 'v2' );
my @all       = ( @physicals, @virtuals );

foreach my $i (0..$#all) {
   my $Var1 = $all[$i];
   my $Var2 = '';
   my @t = ();
   foreach my $n (0..$#all) { 
      next if $i == $n;
      push @t,$all[$n]; 
      $Var2 = join(',',@t);
   }
   print "\$Var1 = $Var1 *** \$Var2 = $Var2 \n";
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks that worked perfect!
Sorry for the late thanks - was on vacation!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top