Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > To store the output of printf into a variable...

Reply
Thread Tools

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

 
 
clearguy02@yahoo.com
Guest
Posts: n/a
 
      04-03-2005
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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


perldoc -f sprintf

jue


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-03-2005
(E-Mail Removed) wrote in
news:(E-Mail Removed) oups.com:

> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments