|There's more than one way to do things|
Splitting/joining on different charactersby SirBones (Friar)
|on May 25, 2006 at 18:25 UTC||Need Help??|
SirBones has asked for the
wisdom of the Perl Monks concerning the following question:
Hey wise-guys/gals... :-). Kind of a two part question, the first fine-tuning, is-there-a-better-slicker-Perl-idiom. The second is a request for some split/join guidance.
I've got a list of names that I need to "normalize" according to the following rules:
I'm promised (I think) that the items will always be space-separated, although I'm a bit nervous about that one. I think I need to allow for that, just in case. In any event, the following code seems to work if I assume just space delimiters:
Gives me exactly what I want:
Part of my learning process with Perl has been to always ask "Is there a better way?" Any ideas? I'd be particularly interested in people pointing out any weaknesses with this approach, and if there is a more concise way to do it.
And back to question 2: Of course this code doesn't work for Claus,Santa (no space delimeter). I'd appreciate any suggestions for handling the case if someone surprises me with only a comma delimeter. I tried splitting on / |,/ but of course that eliminates all the commas in the output. I'm having difficulty since if I'm splitting on either a space or a comma, I don't know how to tell which one forced the split so that I can use the appropriate character when I re-join the whole thing. I hope that makes sense.
"This bounty hunter is my kind of scum: Fearless and inventive." --J.T. Hutt