Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

Stumbled on some code today that I didn't like very much. Two possible improved implementations are shown in the test program below. How would you do it?

use strict; use warnings; # Original function. # Given an input string $buf, return a string of its ord values sub old_dump_buf { my $buf = shift; my $out; for (my $x = 0; $x < length($buf); $x++) { $out .= sprintf("%3d ", ord(substr($buf, $x, 1))); } return $out; } # Possible improved implementations follow. How would you do it? sub new_dump_buf_1 { my $buf = shift; return sprintf '%3d ' x length($buf), unpack( 'C*', $buf ); } sub new_dump_buf_2 { my $buf = shift; return join ' ', map { sprintf '%3d', $_ } unpack( 'C*', $buf ); } my $testdata = join '', map { chr } 0..255; my $old = old_dump_buf($testdata); $old =~ s/ +$//; print $old, "\n"; my $new1 = new_dump_buf_1($testdata); $new1 =~ s/ +$//; $new1 eq $old or die "oops old != new1"; my $new2 = new_dump_buf_2($testdata); $new2 eq $old or die "oops old != new2";


In reply to Function to produce formatted ord values of a string by eyepopslikeamosquito

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 cooling their heels in the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found