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

C

clearguy02

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
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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.

You are welcome.

Sinan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top