in reply to Re: Perl 'grammar'
in thread Perl 'grammar'

Not quite.. :-)
That returns JSmithohn
sub transform { my ($name) = @_; $name =~ s/^\s*(\S+),\s(\S)\S+$/${2}${1}/; # Added '\S+$' return $name; }

Replies are listed 'Best First'.
Re^3: Perl 'grammar'
by dragonchild (Archbishop) on Jan 09, 2008 at 00:00 UTC
    Nice catch. A better version of the regex would be s/^\s*(\S+),\s+(\S).*$/${2}${1}/; - you don't want to assume too much about the item. That's why the ^\s*(\S+) instead of just ^(\S+).

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        Assuming they don't also contain commas and always start and end with a non-space ... s/^\s*(\S(?:[^,]*\S)?)\s*,\s+(\S).*$/${2}${1}/; Update: Fix for ysth's objections.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?