meena has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to create a two dimensional queue in Perl
by BrowserUk (Patriarch) on Aug 04, 2013 at 10:11 UTC | |
A 2-dimensional queue? That would be "a crowd". If you create a crowd, we'll probably need to send in riot control. 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.
| [reply] |
by marinersk (Priest) on Aug 05, 2013 at 16:23 UTC | |
/me needs sleep
| [reply] |
Re: How to create a two dimensional queue in Perl
by Laurent_R (Canon) on Aug 04, 2013 at 08:37 UTC | |
A simple queue can just be an array in which you add new items at the end of the array with the push operator, and remove oldest elements from the front with the shift operator (you could also use pop and unshift, but it seems less natural and it is probably slower). For a "two dimensional queue", although your description seems to be far from clear in my eyes at least (you don't specify how the items should be managed), you could in principle use an array of arrays (AoA). Or maybe what you really need is just several queues working in parallel (with different types of items). Or, if you really think of a queue within the queue, then you should probably just flatten the whole thing into a single queue, which will work just the same way. But the very idea of a two dimensional queue seems to somewhat defeat or contradict the essential idea of a queue, i.e. a FIFO data structure in which the oldest element is removed first. This tends to sound as a case of XY Problem. Please state what you are actually trying to do, rather than asking how to implement what you think is the solution to your problem. | [reply] [d/l] [select] |
by meena (Novice) on Aug 04, 2013 at 09:34 UTC | |
| [reply] |
by Laurent_R (Canon) on Aug 04, 2013 at 10:59 UTC | |
Assuming your question is not related to threads (since nothing of what you said in this ... thread relates to threads), my understanding is that you need a queue of items, in which each item is itself a list of successive actions to be performed together one after the other when a certain state is reached. This is not really a queue of queues, but rather a queue of lists. If such is the case, then an AoA is probably what you need. Something like this:
To add an item to the queue: push @queue, [7, 3, 0, 9];Which results in the following data structure:
To retrieve and remove a list of actions from the queue:
Now, $action_ref is [1, 2, 3], i.e. a reference to an anonymous array containing (1, 2, 3). You can now retrieve the actions one after the other from that anonymous array. For example, if you want to list the actions you've just dequeued: print $_, " - " foreach @$action_ref; # prints: 1 - 2 - 3 -Is this what you need? | [reply] [d/l] [select] |
by james2vegas (Chaplain) on Aug 04, 2013 at 09:38 UTC | |
Alternatively you could use an actual state machine implementation from CPAN. | [reply] |
by BrowserUk (Patriarch) on Aug 05, 2013 at 17:52 UTC | |
I want to write a state machine using queues Meaning :Each state has two or more operations After each operation is there that operation from the given state should be removed ;and only after all operations are done ,the state should be dequeued from the queuue and we should move forward to the next state Let's visualise that:
Will all of state1 (or state9) operations be completed before any of state2 (state8)? If so, just enqueue() a bunch of Thread::Queue objects onto a Thread::Queue. Dequeue() them one at a time and dequeue() each operation one at a time and process it. But that isn't a state machine, it's just a pre-serialised series of operations. If however, it is your intention that the first operation of any of states 1 thru 9 can be processed in any order -- which would make it a state machine -- then you aren't using a queue for the outer level; you are using a random access data structure. Ie. an array. So, use an array of queues rather than abusing a queue of queues. 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.
| [reply] [d/l] |
by marinersk (Priest) on Aug 05, 2013 at 17:09 UTC | |
How to "dimensionalize" your queue greatly depends on how the queue is implemented. A simple queue can be a normal array -- that is to say, an array of scalars:
To dimensionalize this, you could make each entry in the queue an array instead of a scalar -- look up "Array Of Arrays" or AoA. Thought I saw links already embedded in other answers on this thread. If the queue is a hash instead of an array, syntax for use of the queue is simpler, but now you have to manage the hash keys to ensure uniqueness and sequence (presumably FIFO). I suppose you could just use a two-dimensional array, but I think doing so you'd lose the very attractive simplicity of push/shift. I'd probably just use a hash at that point. If I had the privilege of building the two-dimensional queue manager from scratch, the question I'd find most pertinent is whether or not the calling program would benefit from receiving its state steps in an array.
If yes, I'd do the extra work to use AoA.
Good luck! | [reply] [d/l] |
by pemungkah (Priest) on Aug 05, 2013 at 23:36 UTC | |
Do the state transitions add more items to the queues or something like that? Otherwise I can't see why a...oh, WAIT. Is the idea to have a top-level set of states (the "top"), with each state having a "queue" (actually a set) of possible "next" states, and this needs to possibly be variable - or each state is set up to have multiple actions, to be used one-by-one? So the real idea is to have a list of possible current states, with each possible next state linked to it, and one or more actions per each state? More like the right idea? | [reply] |
Re: How to create a two dimensional queue in Perl
by Corion (Patriarch) on Aug 04, 2013 at 07:41 UTC | |
What is a two-dimensional queue? Also see Thread::Queue for a one-dimensional queue. Maybe if you use enough of them, you can get a two-dimensional queue. | [reply] |
by meena (Novice) on Aug 04, 2013 at 08:11 UTC | |
| [reply] |
by BrowserUk (Patriarch) 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.
| [reply] |
by pemungkah (Priest) on Aug 05, 2013 at 02:15 UTC | |
The output will be 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. | [reply] [d/l] [select] |
Re: How to create a two dimensional queue in Perl
by rjt (Curate) on Aug 04, 2013 at 09:53 UTC | |
Based on your previous questions1, you are probably talking about threads. As Corion pointed out, Thread::Queue provides queue functionality to threads. According to its documentation (reformatted for brevity):
Your question here is very vague (please read How do I post a question effectively?), but taking your meaning of "two dimensional queues" as literally as I can, I believe a simple anonymous array ref (constructed with [ ... ]) would work for you. Something like this:
That's pretty much the pattern. You should be able to easily adapt it to your needs. If not, write up a better description of what you need, and we'll help you from there. ____________ | [reply] [d/l] [select] |