Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > foreach - sorted array the way I want?

Reply
Thread Tools

foreach - sorted array the way I want?

 
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      12-10-2009
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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      12-10-2009
On Thu, 10 Dec 2009 16:52:43 +0100, Tomasz Chmielewski <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2009
Tomasz Chmielewski <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-11-2009
Tomasz Chmielewski <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Content reversal-Not working with foreach and pop from array divyajacob Perl Misc 8 06-15-2010 12:25 AM
References to an array in a foreach David K. Wall Perl Misc 14 10-19-2004 04:29 AM
scope of MY in FOREACH with global array Bill Perl Misc 3 07-17-2004 04:17 AM
Idiom for array index that I'm foreach'ing over? Tim Shoppa Perl Misc 45 12-23-2003 04:30 PM
bug in lexically scoped array not reset in foreach loop Gavin Sherlock Perl Misc 1 12-10-2003 10:36 PM



Advertisments