With apologies to Zaxo for continuing to use boolean operators to construct switch statements, I'd appreciate any comments on this code. The sub uFIFO {} rather than the test code below it.

I'd also appreciate any pointers on why the last line of the while loop does nothing?

And the best way of debugging the problem?

Thanks.

#! perl -w use strict; sub uFIFO { my (%fifo, @fifo); my $next = 0; return sub { # no parameters - return the next value or reset the itera +tor 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 retu +rn the count of them or defined $_[0] and return scalar map{ defined $_ && !exists $fi +fo{$_} 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 or +der 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 happ +ens 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>

What's this about a "crooked mitre"? I'm good at woodwork!

In reply to RFC and debugging tips by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.