Thank you, dvinay, for having reformatted your data, this is really more readable and what you want is now very clear. If you don't particularly care about the order in which the output is stored into you output command array, this can be done in basically two lines of code:
use strict; use warnings; my @db= qw /db1 db2/; my @commands =( "HPRMCLI exch create -s EXCHDAGNODE1 ", "HPRMCLI exch mount -h RMEBackupSvr", "HPRMCLI exch validate -level Log", "HPRMCLI exch unmount ", "HPRMCLI exch remove ", "HPRMCLI exch create -s EXCHDAGNODE2 -nc", "HPRMCLI exch mount -h RMEBackupSvr", "HPRMCLI exch validate -level Full", "HPRMCLI exch unmount ", "HPRMCLI exch remove "); my @out_com; for my $comm (@commands) { push @out_com, "$comm -mdb $_" for @db; } print join "\n", @out_com;
which gives the following output:
$ perl commands.pl HPRMCLI exch create -s EXCHDAGNODE1 -mdb db1 HPRMCLI exch create -s EXCHDAGNODE1 -mdb db2 HPRMCLI exch mount -h RMEBackupSvr -mdb db1 HPRMCLI exch mount -h RMEBackupSvr -mdb db2 HPRMCLI exch validate -level Log -mdb db1 HPRMCLI exch validate -level Log -mdb db2 HPRMCLI exch unmount -mdb db1 HPRMCLI exch unmount -mdb db2 HPRMCLI exch remove -mdb db1 HPRMCLI exch remove -mdb db2 HPRMCLI exch create -s EXCHDAGNODE2 -nc -mdb db1 HPRMCLI exch create -s EXCHDAGNODE2 -nc -mdb db2 HPRMCLI exch mount -h RMEBackupSvr -mdb db1 HPRMCLI exch mount -h RMEBackupSvr -mdb db2 HPRMCLI exch validate -level Full -mdb db1 HPRMCLI exch validate -level Full -mdb db2 HPRMCLI exch unmount -mdb db1 HPRMCLI exch unmount -mdb db2 HPRMCLI exch remove -mdb db1 HPRMCLI exch remove -mdb db2
Or, if you prefer to list first all the commands for db1 and then all commands for db2, just modify the relevant lines as follows:
for my $database (@db) { push @out_com, "$_ -mdb $database" for @commands; }
which produces the following output:
$ perl commands.pl HPRMCLI exch create -s EXCHDAGNODE1 -mdb db1 HPRMCLI exch mount -h RMEBackupSvr -mdb db1 HPRMCLI exch validate -level Log -mdb db1 HPRMCLI exch unmount -mdb db1 HPRMCLI exch remove -mdb db1 HPRMCLI exch create -s EXCHDAGNODE2 -nc -mdb db1 HPRMCLI exch mount -h RMEBackupSvr -mdb db1 HPRMCLI exch validate -level Full -mdb db1 HPRMCLI exch unmount -mdb db1 HPRMCLI exch remove -mdb db1 HPRMCLI exch create -s EXCHDAGNODE1 -mdb db2 HPRMCLI exch mount -h RMEBackupSvr -mdb db2 HPRMCLI exch validate -level Log -mdb db2 HPRMCLI exch unmount -mdb db2 HPRMCLI exch remove -mdb db2 HPRMCLI exch create -s EXCHDAGNODE2 -nc -mdb db2 HPRMCLI exch mount -h RMEBackupSvr -mdb db2 HPRMCLI exch validate -level Full -mdb db2 HPRMCLI exch unmount -mdb db2 HPRMCLI exch remove -mdb db2
If the order in which you presented the output is important for you, then it is very slightly more complicated, but really only very slightly. Let us know if the order is important.
Update: looking back at the original question, I noticed that you probably don't actually need to store the commands in a new array (as I thought you wanted). If you just want to print out the commands, it is even easier. Remove the declaration of the @out_com array and change the relevant code to this:
for my $comm (@commands) { print "$comm -mdb $_ \n" for @db; }
In reply to Re: Perl array output sorting
by Laurent_R
in thread Perl array output sorting
by dvinay
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |