dvinay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i am struck with some array sorting here, i have an requirement where i have a 2 arrays namely @db (which contains database names) and other one @Commands (which contains commands array) now i need to form one single array which contains commands with all the db names in array1

For example

@db=("db1, "db2") @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 ");

at the end i need output something like this

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 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 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 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

my small piece of code goes as below

use strict; use warnings; my @dbs = ("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 "); for my $db (@dbs) { for my $command (@commands) { print("\ $command -mdb $db\n"); } }

Any help regarding this would be appreciated Thanks, Vinay

Replies are listed 'Best First'.
Re: Perl array output sorting
by LanX (Saint) on Oct 31, 2013 at 18:39 UTC
    I think you are looking for push instead of print.

    push @newarray,"$command -mdb $db"
    Though I don't understand what it has to do with "sorting" (?) Otherwise plz rephrase your question.

    And please put your data also in code tags!!!

    (it's almost unreadable now. =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Perl array output sorting
by hdb (Monsignor) on Oct 31, 2013 at 19:43 UTC

    I love to use glob for such an exercise:

    use strict; use warnings; use File::Glob 'bsd_glob'; my @dbs = ("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 @cmds = bsd_glob "{@commands} -mdb {@dbs}"; print "$_\n" for @cmds;

    Update: Of course -- as the Perl Puritans did point out -- the use of any special variable should be localized:

    my @cmds = do { local $"=","; bsd_glob "{@commands} -mdb {@dbs}" };
      some Perl puritians wouldn't even use do

      DB<107> my @cmd; { local $"=','; @cmds = < "{@commands} - {@dbs}" > +} => ( "HPRMCLI exch create -s EXCHDAGNODE1 - db1", "HPRMCLI exch create -s EXCHDAGNODE1 - db2", "HPRMCLI exch mount -h RMEBackupSvr - db1", "HPRMCLI exch mount -h RMEBackupSvr - db2", "HPRMCLI exch validate -level Log - db1", ...

      update

      > the use of any special variable should be localized:

      not any just the ones potentially creating global damage ... =)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: Perl array output sorting
by Laurent_R (Canon) on Oct 31, 2013 at 22:30 UTC

    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:

    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; }

      hi monk,

      Thanks for the previous code, as i had told in my question, the order in which i have posted is very important to me, the above output for your code snippet was generated by me previously itself, but my requirement became little tweaked, so output which i posted at last is must, if you help me in that regard it would be really great..

      for your help i am again posting the required output below

      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 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 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 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

      Thanks, Vinay
Re: Perl array output sorting
by Laurent_R (Canon) on Oct 31, 2013 at 18:46 UTC

    Yesssss, please put your data in code tags to keep the formatting.

      Not that I particularly care, but I wonder why I got a down vote for having written my above post. I just can't really make sense of the input data and of the expected result in the way these are currently formatted. But perhaps the problem has to do more with the person who downvoted my post than with the content of my post.

        I suspect you are correct in your last statement.

        Best guess I would have is that someone perhaps thought it looked like an advanced "me too" post and they have a bug up their behind about those or something.

        It's a reach, I admit, but the only thing I could think of.

        Otherwise, probably just someone being a bit of a snot.

        I got downvoted earlier today for admitting I didn't know something.  LOL. Who knows what goes through peoples' minds?

Re: Perl array output sorting
by Laurent_R (Canon) on Nov 02, 2013 at 16:14 UTC

    OK, this is a version trying to output the commands in the order you described. Since you only gave an example but did not give any specific rules as to how to get to the result in your example, I based the code on the idea that the DB sequence break must occur after each occurrence of a record containing "exch remove".

    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 (@commands) { push @out_com, $_; if (/exch remove/) { for my $database (@db) { print map {"$_ -mdb $database \n"} @out_com; } @out_com = (); } }
    This gives 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 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 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 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
    which I think is what you are looking for. I kept the @out_com array, but this time only as a temporary array to store and group the lines needing to go together between the db breaks.

      Thanks a lot Laurent_R for the timely help, i got the required output.

      Once again thanks a lot.

      Thanks,

      Vinay