foreach - sorted array the way I want?

Discussion in 'Perl Misc' started by Tomasz Chmielewski, Dec 10, 2009.

  1. I have a script which reads the config file and builds the commands
    which need to be executed.

    It pushes each command to an array.

    When everything is read from the config file, it is executed like this:


    foreach my $exec_command (@exec_commands) {
    execute($exec_command);
    }


    However, such approach does not guarantee that the commands will be
    executed in a proper order.


    Supposing I would like to have the guarantee that the commands
    containing "someword" will be executed before commands containing
    "someotherword" (and this, before "xyz" command), what would be the best
    approach here?


    1) build a hash, not an array in the first place? I'm afraid it would
    complicate it too much.


    2)


    foreach my $exec_command (@exec_commands) {
    if ($exec_command=~m { .... }) {
    push(@execute_first, $exec_command);
    }

    if ($exec_command=~m { .... }) {
    push(@execute_second, $exec_command);
    }

    }

    And then, simply execute commands from @execute_first, @execute_second,
    and so on?


    3) something else?


    --
    Tomasz Chmielewski
    http://wpkg.org
     
    Tomasz Chmielewski, Dec 10, 2009
    #1
    1. Advertising

  2. Tomasz Chmielewski

    Guest

    On Thu, 10 Dec 2009 16:52:43 +0100, Tomasz Chmielewski <> wrote:

    >I have a script which reads the config file and builds the commands
    >which need to be executed.
    >
    >It pushes each command to an array.
    >
    >When everything is read from the config file, it is executed like this:
    >
    >
    >foreach my $exec_command (@exec_commands) {
    > execute($exec_command);
    >}
    >
    >
    >However, such approach does not guarantee that the commands will be
    >executed in a proper order.
    >
    >
    >Supposing I would like to have the guarantee that the commands
    >containing "someword" will be executed before commands containing
    >"someotherword" (and this, before "xyz" command), what would be the best
    >approach here?
    >
    >

    [snip]

    >And then, simply execute commands from @execute_first, @execute_second,
    >and so on?
    >
    >
    >3) something else?


    That would work but its a little too specific.
    Here is another way to do the same thing (there are many).

    -sln

    -------------
    use strict;
    use warnings;

    my @cmd_priority = (
    'cmd3',
    'cmd4|cmd5',
    'cmd1|cmd2',
    );

    my @exec_commands =
    qw{ cmd2 cmd3 cmd5 cmd3 cmd1 cmd4 };

    for my $cmd
    ( sort {
    my $res = 0;
    for (reverse @cmd_priority) {
    $res ||= ($a =~ /$_/ <=> $b =~ /$_/);
    }
    $res;
    } @exec_commands )
    {
    print "$cmd\n";
    # execute($cmd);
    }
    __END__

    Output:

    cmd3
    cmd3
    cmd5
    cmd4
    cmd2
    cmd1
     
    , Dec 10, 2009
    #2
    1. Advertising

  3. Tomasz Chmielewski <> writes:
    > I have a script which reads the config file and builds the commands
    > which need to be executed.
    >
    > It pushes each command to an array.
    >
    > When everything is read from the config file, it is executed like this:
    >
    >
    > foreach my $exec_command (@exec_commands) {
    > execute($exec_command);
    > }
    >
    >
    > However, such approach does not guarantee that the commands will be
    > executed in a proper order.
    >
    >
    > Supposing I would like to have the guarantee that the commands
    > containing "someword" will be executed before commands containing
    > "someotherword" (and this, before "xyz" command), what would be the
    > best approach here?
    >
    >
    > 1) build a hash, not an array in the first place? I'm afraid it would
    > complicate it too much.
    >
    >
    > 2)
    >
    >
    > foreach my $exec_command (@exec_commands) {
    > if ($exec_command=~m { .... }) {
    > push(@execute_first, $exec_command);
    > }
    >
    > if ($exec_command=~m { .... }) {
    > push(@execute_second, $exec_command);
    > }
    >
    > }
    >
    > And then, simply execute commands from @execute_first,
    > @execute_second, and so on?
    >
    >
    > 3) something else?


    You might consider sorting the @exec_commands array, using a carefully
    constructed comparison function.

    Note that sorting is O(n*log n), whereas your approach 2 is O(n); if
    the @exec_commands isn't too long this won't be too much of an issue.

    You should also think about whether you have or need stable sorting
    (i.e, sorting that preserves the original ordering for elements that
    compare equal). "perldoc -f sort" and "perldoc sort" for more
    information.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 11, 2009
    #3
  4. Tomasz Chmielewski <> wrote:
    [...]
    >foreach my $exec_command (@exec_commands) {
    > execute($exec_command);
    >}
    >However, such approach does not guarantee that the commands will be
    >executed in a proper order.


    Well, they will be executed in the exact sequence in which they are
    stored in @exec_commands. If that is not the desired sequence, then ...

    >Supposing I would like to have the guarantee that the commands
    >containing "someword" will be executed before commands containing
    >"someotherword" (and this, before "xyz" command), what would be the best
    >approach here?


    .... why don't you simple sort() the commands into the proper sequence?

    jue
     
    Jürgen Exner, Dec 11, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    639
    Chris Smith
    Sep 25, 2003
  2. Aaron Watters

    Best way to merge/sort two sorted lists?...

    Aaron Watters, Dec 6, 2007, in forum: Python
    Replies:
    11
    Views:
    1,006
    Paul Rubin
    Jan 11, 2008
  3. SherjilOzair

    Best way to insert sorted in a list

    SherjilOzair, Jun 17, 2011, in forum: Python
    Replies:
    10
    Views:
    1,173
    Vito De Tullio
    Jun 19, 2011
  4. Tim Shoppa
    Replies:
    45
    Views:
    545
    Darren Dunham
    Dec 23, 2003
  5. Gavin Sherlock
    Replies:
    1
    Views:
    107
    Uri Guttman
    Dec 10, 2003
Loading...

Share This Page