Monks ~
My CGI script is going to accept uploaded files and then put them somewhere. I don't want to overload one directory, so I'm using a three-level structure: "a/aa/aaa" to "z/zz/zzz". This way no directory has over 26 sub directories, no directory has more than $MAX (constant) files, and there are 26^3 possible directories.
my( $p1, $p2, $p3 ); # path parts, each one character, from database
my( $count ); # number of directory items, from database
if( $count > $DIR_MAX_ENTRIES ) {
if( $p3 eq 'z' ) {
$p3 = 'a';
if( $p2 eq 'z' ) {
$p2 = 'a';
$p1 = chr( ord( $p1) + 1 );
} else {
$p2 = chr( ord( $p2) + 1 );
}
} else {
$p3 = chr( ord( $p3) + 1 );
}
}
It works as expected, with two
caveats:
- If the first character passes 'z' it barfs. This is fine: if we ever approach 'z' then the system will need redesign for other reasons.
- '$count' may be susceptible to a race condition, since it's a stored number rather than an actual count of directory entries. This is fine: it's not meant to be high-precision, and $MAX will be low enough that some slop is ok.
Finally, though, I wonder if there's a faster or simpler way to do this. I'd thought about using an array of characters (rather than 'ord()' and 'chr()') and incrementing the subscript. This wouldn't eliminate the 'if eq z' check, though, just change its form and move it a bit.
Any advice from the monks? Thanks!
--
man with no legs, inc.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.