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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.