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:

#!/usr/bin/perl use strict; use warnings; my @names = ("Foonman, Joseph S. (Joe)", "SMITH, EDWARD", "Perl, Paul M. *CONTRACTOR*", "Jones, Bobby R.", "Ruth, BABE B. *CONTRACTOR*", "CLAUSE, SANTINO (SANTA)", ); for my $n (@names) { print join(" ", map( { if (/^\*/) { $_; } elsif (/^\(/) { "\(".ucfirst(lc(substr($_,1))); } else { ucfirst(lc($_)); } } split(/ /,$n)))."\n"; }

Gives me exactly what I want:

Foonman, Joseph S. (Joe) Smith, Edward Perl, Paul M. *CONTRACTOR* Jones, Bobby R. Ruth, Babe B. *CONTRACTOR* Clause, Santino (Santa)

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.

Cheers,
Ken

"This bounty hunter is my kind of scum: Fearless and inventive." --J.T. Hutt

In reply to Splitting/joining on different characters by SirBones

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!
  • 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:
    & &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.