in reply to How do I use regex to strip out specific characters in a string?

I wouldn't use regexes for this, in large part because they're harder for people to understand, even if well commented. Instead, I'd do something like:
my ($day_of_week, $date, $time) = split ' ', $stamp; my ($month, $day_of_month, $year) = split '/', $date; $time =~ s/://g; my $munged_stamp = "${month}_${day_of_month}_${time}";

------
/me wants to be the brightest bulb in the chandelier!

Vote paco for President!

  • Comment on Re: How do I use regex to strip out specific characters in a string?
  • Download Code

Replies are listed 'Best First'.
Re: Re: How do I use regex to strip out specific characters in a string?
by Sifmole (Chaplain) on Aug 23, 2001 at 00:30 UTC
    Doing it this way causes the creation of 7 temporary variables.

    You also should use a tr when you are only trying to affect a single character:

    tr/://d; # instead of s/://g;
    runrig below gave you a very elegant solution using substr and tr
      I agree with the tr/// instead of the s///.

      But, the creation of temp variables shouldn't be an issue. But, if you're that concerned with the creation of temporary variables, just undef the variables after you're done. You could even combine the variables as such:

      { my @temp_variables; push @temp_variables, split ' ', $stamp; push @temp_variables, split '/', $temp_variable[1]; $temp_variables[2] =~ tr/://d; $stamp = join '_', @temp_variables[3,4,2]; undef @temp_variables; }

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!

        Temporary variables can clutter things up, and cause confusion. You solution seems to start leaning more towards the confusing side as well. I know that I personally would want to see a good regular expression rather than a bunch of array slice uses.

        Perhaps it is just personal preference.