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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found