your "new ids should be as similar as possible to the old ones" is poorly defined and -sorry - your code is far too undocumented to be read or published (especially for such a delicate use case)
I'll give you a concept instead of code:
For what I understand you need 5 steps
- split /_/
- split /(\d+)/
- count nondigit substrings in a hash %strings
- shorten keys of this hash "as similar as possible"
- recompose
The magic is of course in step 4 (and you have to care about about different lengths of numbers in step 2)
for instance you could start to investigate the first n chars of each \w+ string in a loop.
As long as a grep(/$shortend\w*/) keys %strings counts more than 1 match you have to try again with a larger $n.
you can extend this method to grep(/$pre\w*$post/) ...
To further shorten the recomposed string you might consider using CamelCase instead of _ as delimiter (i.e. uppercase first character)
To be clear, all of this doesn't guaranty len(strings) <= $length, so you might be forced to skip readability for some keys in an extra step of shortening.
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.