harmony has asked for the wisdom of the Perl Monks concerning the following question:

I have a variable, batch_date, which is the
SYSDATE
selected with the format
MM/DD/YYYY HH24:MI:SS
. How can I take the string
$batch_date
and remove all of the blank spaces, then put this into a new variable?

Replies are listed 'Best First'.
Re: How do I remove all of the blank spaces in a string?
by wog (Curate) on Jun 08, 2001 at 01:38 UTC
    ($a_new_variable = $batch_date) =~ tr[ \n\t][]d;

    This should be slightly faster then s///.

Re: How do I remove all of the blank spaces in a string?
by marcink (Monk) on Jun 08, 2001 at 01:36 UTC
    Use a regexp:
    $batch_date =~ s/\s//g;

    Or, if you want to keep the old value in input variable:
    ($new_var = $batch_date) =~ s/\s//g;


    -mk
Re: How do I remove all of the blank spaces in a string?
by tomhukins (Curate) on Jun 08, 2001 at 01:36 UTC

    From your question, I'm not clear what you're trying to achieve. Could you give more detail?

    If you just want to remove spaces from a variable, try $batch_date =~ s/\s//g;.

      Thank you everybody for the help you've given on this.
      But now I need to perform additional work on this string. After removing the spaces, I need to also remove the slashes
      (/)
      . I know how to remove the colons
      (:)
      . It's tricky, with
      s/ / /g
      since
      /
      are the separators.
        As usual, there's a wide choice of ways to do this ;-) A couple of ways of converting slashes to letters 'q':

        Method one: backslash
        s/\//q/g

        Method two: cool s/// syntax
        s|/|q|g
        s(/)(q)g
        s{/}{q}g
        You don't have to use slashes in s/// -- take a look at 'man perlop'.

        Update: Are you sure you don't want to take a look at Date::Manip and Date::Format? There's a nice parseDate function in the former ;-)

        And one more s/// that probably does exactly what you want, in one pass:
        s{(\d\d)/(\d\d)/(\d\d\d\d)\s+(\d\d):(\d\d):(\d\d)}{$1$2$3$4$5$6}

        -mk