I'm assuming that these aren't priority queues - just items in whatever order they've been added into the queues. So here's how to go about it.
# Start with an empty master queue.
my @master;
# Build a sub queue. Note that this is a array *reference*.
my $subqueue = [ 1, 2, 3, 4];
# add it to the queue. push() puts it at the tail of the queue.
push @master, $subqueue;
# Now let's create a couple more sub queues and add them.
push @master, [5, 6, 7];
# Some high-priority-items we'll stick at the front of the master:
unshift @master, [-1, 0];
# Now let's run the queue. We look at the master. If there are no item
+s remaining,
# the queue is empty. Otherwise, process the leading sub queue.
while (@master) {
# If the leading sub queue is empty, discard it, and start over with
+ the rest of
# the (now possibly empty) master queue.
while ( @{ $master[0] } ) {
my $item = shift @{ $master[0] };
print $item, " ";
}
# We arrive here when the sub queue is empty. Discard the empty sub
+queue.
shift @master;
# Move to the next line so we can see we switched sub queues.
print "\n"
}
print "\n";
The output will be
-1 0
1 2 3 4
5 6 7
Obviously this is a toy program, but it serves to demonstrate the basic array operations and tests that are needed to manage a queue of queues. More complex operations (like priority queuing, etc.) are left as an exercise. (I'd probably switch the plain scalar values for anonymous hashes, one item of which is a 'priority' field that you can sort the array of hashes on, and the other a 'value' which will be either the scalar value you want in the node (on the sub queues) or a reference to a subqueue (for the master).)
And of course this isn't a class; that'd be a nice thing to do as well.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.