in reply to Re: How would you rewrite this?
in thread How would you rewrite this?

Sweet. May I ask what is the process you used to come up with your solution? Is it just experience?

Replies are listed 'Best First'.
Re^3: How would you rewrite this?
by bv (Friar) on Aug 30, 2009 at 20:29 UTC

    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. :-)