To store the output of printf into a variable...

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Apr 3, 2005.

  1. Guest

    Hi experts,

    I got help earlier from here on a scrit and the final script looks as
    follows. My goal is to store the new date into a vairable and use it
    some where else in the perl program:

    -----------------------

    $date = '15-Mar-04.01:03';
    %months = qw(Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct
    10 Nov 11 Dec 12);
    $month_lookup = join '|', keys %months;
    if ( $date =~ /(\d+)-($month_lookup)-(\d+)\.(\d+):(\d+)/ )
    {
    printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1, $4,
    $5;
    }
    -------------------------------

    Now, instead of printing the date onto the cmd screen, I want to store
    it into a variable.

    When I do the following, it doesn't work:
    $finalDate = printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2},
    $1, $4, $5;
    And $finalDate doesn't capture the date.

    I also tried the below one:
    printf $finalDate "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2},
    $1, $4, $5;

    Can you pl. tell me how I can store the date into a variable instead of
    printing it onto the screen?

    Thanks,
    John
     
    , Apr 3, 2005
    #1
    1. Advertising

  2. wrote:

    > [Subject:] To store the output of printf into a variable...


    perldoc -f sprintf

    jue
     
    Jürgen Exner, Apr 3, 2005
    #2
    1. Advertising

  3. wrote in
    news::

    > Hi experts,


    Oh, please!

    > I got help earlier from here on a scrit and the final script looks as
    > follows. My goal is to store the new date into a vairable and use it
    > some where else in the perl program:


    Have you read the posting guidelines for this group yet?

    use strict;
    use warnings;

    > $date = '15-Mar-04.01:03';


    my $date = '15-Mar-04.01:03';

    > %months = qw(Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct
    > 10 Nov 11 Dec 12);


    my %months = ...;

    > $month_lookup = join '|', keys %months;
    > if ( $date =~ /(\d+)-($month_lookup)-(\d+)\.(\d+):(\d+)/ )
    > {
    > printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1, $4,
    > $5;
    > }


    As I have mentioned before, there is a Y2.1K bug in the line above.

    > Now, instead of printing the date onto the cmd screen, I want to store
    > it into a variable.
    >
    > When I do the following, it doesn't work:
    > $finalDate = printf "20%02d-%02d-%02d %02d:%02d:00\n", $3,
    > $months{$2}, $1, $4, $5;
    > And $finalDate doesn't capture the date.


    Of course not!

    Why not consult the documentation for printf to see what it returns?

    perldoc -f printf

    printf FILEHANDLE FORMAT, LIST
    printf FORMAT, LIST
    Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", except
    that $\ (the output record separator) is not appended. The first
    argument of the list will be interpreted as the "printf" format.
    See "sprintf" for an explanation of the format argument.
    ...

    Then you say to yourself "Hmmm ... I wonder what sprintf does"

    perldoc -f sprintf

    sprintf FORMAT, LIST
    Returns a string formatted by the usual "printf" conventions of

    Oh my, oh my! Problem solved without asking hundreds of other people.

    > I also tried the below one:
    > printf $finalDate "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2},
    > $1, $4, $5;


    Here is some good advice:

    <URL: http://jwenet.net/notebook/2005/1036.html>

    The first two items are very pertinent to your query.

    > Can you pl. tell me how I can store the date into a variable
    > instead of printing it onto the screen?


    ITYM 'please'.

    FYI, printf does not print to the screen, but rather to STDOUT (by
    default) or the last selected filehandle.

    > Thanks,


    You are welcome.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    633
    Martin Ambuhl
    Jun 26, 2004
  2. Replies:
    2
    Views:
    548
    Thomas Matthews
    Apr 28, 2005
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,272
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    444
  5. guru
    Replies:
    8
    Views:
    288
Loading...

Share This Page