#! perl -w use strict; sub uFIFO { my (%fifo, @fifo); my $next = 0; return sub { # no parameters - return the next value or reset the iterator and return undef if its the last. !@_ and return (scalar @fifo && $next < @fifo) ? ${$fifo[$next++]} : ($next=0, undef) # we have parameters and the first one is defined, # push any defined values not already in the list and return the count of them or defined $_[0] and return scalar map{ defined $_ && !exists $fifo{$_} and push @fifo, \($fifo{ $_ } = $_) } @_ # We only get this far if the first value is undef! # reset iterator or lc($_[1]) eq 'reset' and $next = 0 # return all values possibly random order or lc($_[1]) eq 'all' and return keys %fifo # return all values possibly random order or lc($_[1]) eq 'ordered' and return map { ${ $_ } } @fifo # for testing only or lc($_[1]) eq 'dump' and return @fifo ; } } my $fifo1 = uFIFO(); # create a fifo - we can have as many as we need # create some test data and shuffle it my @test = ('a' .. 'z', 'i' .. 'r'); ($a=$_+rand @test-$_) and @test[$_,$a] = @test[$a,$_] for (0..$#test); printf "%15.15s: %s\n", 'Initial data', "@test"; # data in original order for comparison later $fifo1->( $_ ) for @test; #add some test data # Show only one copy of each made it in print sprintf( "%15.15s: ", 'Added'), "@{[$fifo1->( undef, 'all')]} ", $/; # process the list, randomly adding some more and printing them out as we process my $i; printf '%15.15s: ', 'Processed'; while( my $temp= $fifo1->() ) { print $temp, ' ' ; (0 == int(rand 4)) and $fifo1->( ++$i ); # Randomly add new values (0 == int(rand 4)) and $fifo1->( chr(97+rand 26) ); # nothing happens here???? } print $/; print sprintf( "%15.15s: ", 'Ordered'), "@{ [ $fifo1->(undef, 'ordered') ] }", $/; print sprintf("%15.15s: ", 'Unordered'), "@{ [ $fifo1->(undef, 'all') ] }" , $/; __END__ #output C:\test>193450 Initial data: j h m q f l a w p k u b d n t o r i k o q s l p c x y n i m r z e g v j Added: a b c d e f g h i j k l m n o p q r s t u v w x y z Processed: j h m q f l a w p k u b d n t o r i s c x y z e g v 1 2 3 4 5 6 7 8 9 10 11 12 13 Ordered: j h m q f l a w p k u b d n t o r i s c x y z e g v 1 2 3 4 5 6 7 8 9 10 11 12 13 Unordered: a b c d e f g h i j k l m n 1 o 2 p 3 q 10 4 r 11 5 s 12 6 t 13 7 u 8 v 9 w x y z C:\test>