Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How would I change a10 into A:10 in perl?

by lidden (Curate)
on Dec 03, 2010 at 00:58 UTC ( #875047=note: print w/ replies, xml ) Need Help??


in reply to How would I change a10 into A:10 in perl?

It can be done in one line:

$combined =~ s/(\w)(\d+)/uc($1) . ":$2" /e;


Comment on Re: How would I change a10 into A:10 in perl?
Download Code
Replies are listed 'Best First'.
Re^2: How would I change a10 into A:10 in perl?
by Tails (Novice) on Dec 03, 2010 at 01:07 UTC
    Thank you very much! I just began learning regular expressions so I was avoiding it ^^;.
      It's hard to learn something by avoiding it!
Re^2: How would I change a10 into A:10 in perl?
by bart (Canon) on Dec 03, 2010 at 13:31 UTC
    Though it's a good attempt, I'm not too impressed.
    • If the requirements change from 1 letter to more, your code won't handle it.
    • Your right hand side for the substitution is uglier than it should be.

    My proposed fix:

    $combined =~ s/([a-z]+)(\d+)/\U$1:$2/i;

          "If the requirements change from 1 letter to more, your code won't handle it. "

      If the requirements change from letters to kanji or hiragana characters, *your code* won't handle it either. And that is just as likely.

      Coding now to exceed the known requirements, in order to cater for some future, speculative change, is a waste of time and effort.

      When (if!) the requirements change, the the code can be changed to match the new requirements.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://875047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-07 23:33 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