in reply to How would you rewrite this?

My take:

(my $chosen_day = "April 1, 2009") =~ s/\s\s/ /;

Of course, depending on what you are trying to sanitize, you might want to throw a /g on there, or use s/\s+/ /, or something.

$,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;

Replies are listed 'Best First'.
Re^2: How would you rewrite this?
by Anonymous Monk on Aug 30, 2009 at 20:20 UTC
    Sweet. May I ask what is the process you used to come up with your solution? Is it just experience?

      Sure. Sorry I didn't put an explanation in the first post.

      First, you are trying to get around the problem that you can't use s/// on a constant value (literal string "April 1, 2009"), so my first thought was this two-liner:

      my $chosen_day = "April 1, 2009"; $chosen_day =~ s/\s\s/ /;

      In case you haven't seen it, the =~ operator binds the string on the left to the pattern on the right (see perlop). But you asked for a one-liner, so I took advantage of the fact that the return value of an assignment can be bound to a pattern as well:

      # Parenthesized assignment is bound to the substitution (my $c = "April 1, 2009") =~ s/\s\s/ /;
      $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
        Thanks. :-)