Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In the old unexplored code at work, I discovered a subroutine id2path that converts a numeric id of an object to a path (where some content related to the object is stored). I did not like the way how it was implemented, and adding strictures, underlines, and whitespace did not make it much better:

sub id2path { my $id = shift; return q() unless $id; my $path; if ($id > 999_999) { $path = sprintf '%03d/%03d/%03d', $id / 1_000_000, ($id / 1_000) % 1_000, $id % 1_000; } else { $path = sprintf '%02d/%02d/%02d', $id / 10_000, ($id / 100) % 100, $id % 100; } return $path; }

I do not like strange constants and repeated code. However, the only alternative I was able to write was the following:

sub id2path_new { my $id = shift or return q(); my $chunk_length = length $id > 6 ? 3 : 2; $id = sprintf '%0' . ($chunk_length * 3) . 'd', $id; my $chunk = ".{$chunk_length}"; my $path = join '/', $id =~ / ^ (.*) ($chunk) ($chunk) $ /xg; return $path; }

I do not like it much either. Do you have any ideas?

To make your work easier, here is how I tested it:

use Test::More tests => 23; is(id2path_new($_), id2path($_), "id=$_") for q(), 0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 99999, 100000, 999999, 1e6, 1e7-1, 1e7, 1e8-1, 1e8, 1e9, 1e10, 1e11, 1e12;

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Can you make it nicer? by choroba

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (14)
    As of 2015-07-07 20:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls