SPUG: "Programming Challenge"

Michael R. Wolf MichaelRWolf at att.net
Wed Dec 17 23:09:59 CST 2003

Ross Wolin <rowol at copper.net> writes:


>     #Method 3 (same as #1, but buffered writes)
>     #Now interleave the low and high bytes and write to STDOUT,
>     #buffering the write in memory first
>     my $buf;
>     while ($LSB =~/(.)/g) {
>        $buf .= $1;
>        $MSB =~/(.)/g;
>        $buf .= $1;
>     }
>     syswrite(STDOUT, $buf, length($buf));

Caveat emptor.  All code is untested.

You can get rid of the regexp keeping track of pos() via the /g by
spliting it all at once.

    @lsb = split //, $lsb;
    @msb = split //, $msb;

    die "Oops. Bad balance." unless @lsb == @msb;

    while (@lsb && @msb) {
        print pop @lsb, pop $msb;

Anyone remember Perl6 enough to do this with parallel iterators?
Something like this....

    for @lsb ; @msb -> $lsb; $msb {
        print $lsb, $msb;

Or (sorry, Tim... sometimes I like it the other way around)

    print $lsb, $msb for split //, $LSB ; split //, $MSB -> $lsb ; $msb;

Well, perhaps not in *this* case.....

But then again, I think this has a parsing problem. Is ";" the
expression terminator or the stream separator? I guess we'll have to
wait and see.

Michael Wolf

P.S. Did you like my use of "remember" to reference the future?

    It's a poor sort of memory that only works backward.
      -- Lewis Carroll

Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net

More information about the spug-list mailing list