Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How to create a two dimensional queue in Perl

by meena (Novice)
on Aug 04, 2013 at 08:11 UTC ( #1047775=note: print w/ replies, xml ) Need Help??


in reply to Re: How to create a two dimensional queue in Perl
in thread How to create a two dimensional queue in Perl

Each queue element should have two elements which can individually be dequeued Like a queue within a queue


Comment on Re^2: How to create a two dimensional queue in Perl
Re^3: How to create a two dimensional queue in Perl
by pemungkah (Priest) on Aug 05, 2013 at 02:15 UTC
    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.

Re^3: How to create a two dimensional queue in Perl
by BrowserUk (Pope) on Aug 05, 2013 at 05:34 UTC
    Each queue element should have two elements which can individually be dequeued Like a queue within a queue

    If "each queue element contains 2 elements that can be individually dequeued"; what is the difference between a your 2D-queue containing one, two-element element; and a normal queue containing two, single-element elements pushed by the same producer at the same time?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047775]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (16)
As of 2014-12-18 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls