Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Laurent_R (Canon)
on Aug 04, 2013 at 10:59 UTC ( [id://1047785]=note: print w/replies, xml ) Need Help??


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

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:

my @queue = ( [$action1, $action2, $action3], [$action4, $action1, $ac +tion3], ...); # or possibly; my @queue = ( [1, 2, 3], [4, 1, 3]); # where [1, 2, 3] is a reference to an array of subscripts on an array + of actions defined somewhere else.

To add an item to the queue:

push @queue, [7, 3, 0, 9];

Which results in the following data structure:

0 ARRAY(0x80359f90) 0 1 1 2 2 3 1 ARRAY(0x8035a050) 0 4 1 1 2 3 2 ARRAY(0x803561e8) 0 7 1 3 2 0 3 9

To retrieve and remove a list of actions from the queue:

my $action_ref = shift @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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2024-04-16 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found