in reply to Perl 'grammar'

In C, you have to work with each item in itself. With Perl, you work with the whole thing at once. So, you have an idea of what you're starting and where you want to go.
# Starting with Lastname, Firstname # Wanting to get to FLastname sub transform { my ($name) = @_; $name =~ s/^\s*(\S+),\s(\S)/${2}${1}/; return $name; }
So, grab what you want from what you're given, transform it into what you want, then return it. It's often best to do this sort of thing in a function because it's easier to be destructive and smaller scopes give you greater flexibility. You don't have to watch out for anyone else's feelings. :-)

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?

Replies are listed 'Best First'.
Re^2: Perl 'grammar'
by nedals (Deacon) on Jan 08, 2008 at 23:41 UTC
    Not quite.. :-)
    That returns JSmithohn
    sub transform { my ($name) = @_; $name =~ s/^\s*(\S+),\s(\S)\S+$/${2}${1}/; # Added '\S+$' return $name; }
      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?