Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How can I change the format of dates 12/23/02 to 23/12/2002 or 03/35/55 to 35/03/2055?
I can read the dates from text file but I can't change their format:
15/06/17 ====> 06/15/2017
Here's the example using DateTime::Format::Strptime:
UPDATE:
use DateTime::Format::Strptime;
my $str = '12/23/02';
my $parser = DateTime::Format::Strptime->new( pattern => '%m/%d/%y');
my $dt = $parser->parse_datetime( $str );
print $dt->strftime('%d/%m/%Y')
output:
23/12/2002
I think the Perl Module Time::Piece should work
use Time::Piece 'strptime';
my %date = strptime('%d/%m/%y', '15/06/17');
The date hash contains then the parsed date elements, $date{d}, $date{m}, and $date{y} which you can use to reformat the date.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to convert --> Mar 11 10:29:47 2021 GMT <-- into epoch on multiple platforms.
Date::Parse module is not available by default on all platforms.
Use the strptime() ("string parse time") method from Time::Piece which has been a part of the standard Perl distribution since 5.10.0 in 2007.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $str = 'Mar 11 10:29:47 2021 GMT';
my $fmt = '%b %d %H:%M:%S %Y %Z';
my $date = Time::Piece->strptime($str, $fmt);
say $date->epoch; # prints 1615458587
strptime is a standard Unix tool which is available in many forms. You can get more information about the format strings that it uses from its manual page.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the difference between sprintf and printf in Perl?
I'm really confused with those functions.
I know about printf. It is used for STDOUT, but I want know in depth of these functions.
sprintf just returns a formatted string, printf prints it to a filehandle.
printf HANDLE "%s", $arg
can (very redundantly) be written as
$formatted = sprintf "%s", $arg
print HANDLE $formatted
Of course, this specific example is most naturally written as
print HANDLE $arg
because the format string I used for an example is so trivial as to be useless.
Of course, HANDLE is optional, and defaults to STDOUT, although you can also change the default with select.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose I have a sentence/paragraph:
This cat is very cute.
Here, "cute" is the 5th word in the sentence. If I know the index of the first letter of this word - in this case c, 17 - how can I find out the position of this word in the sentence?
Counting the number of spaces in the substring and then adding 1 to it would probably work.
#!/usr/bin/perl
use strict;
use warnings;
my $in = "This cat is very cute.";
my $sub = substr $in, 0, 17;
my $word_count = scalar(split " ", $sub) + 1;
print "$word_count\n";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I want 0 replacing '०' Unicode character. I am using a Perl script and trying the code given below.
my %table_digits =
(
'०' => '0'
'१' => '1', .....)
It's working fine with other Unicode characters. Those are being replaced by other numbers. But it is not able to replace '०' with 0. How it can be done?
See Unicode numeric value in Unicode::UCD:
use 5.010;
use utf8;
use open ':std', ':utf8';
use Unicode::UCD qw(num);
for my $digit (qw( ० १ २ ३ ४ ५ ६ ७ ८ ९ )) {
say "$digit==".num($digit);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to append system date to a filename in perl ?
As i am very new to perl programming , can you give me a simple example for the above query.
The strftime() function from the POSIX module gives an easy way to get a date in whatever format you want.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use POSIX 'strftime';
my $date = strftime '%Y-%m-%d', localtime;
say $date;
You can then use that string in the filename of your file. If you are renaming a file, then you can use move() from the File::Copy module.
This might help u out!
#!/usr/bin/perl
my $date=`date +%Y%m%d`;
chomp($date);
my $source_file="/tmp/fileName_.tgz";
my $destination_file="/misc/fileName_" . $date . ".tgz";
print "$source_file\n";
print "$destination_file\n";
system("sudo mv /tmp/fileName_.tgz /misc/fileName_$date.tgz");
or try this
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
my $out = "$dir/$host-$mday-$mon-$year"
or this one
# grab the current time
my #now = localtime();
# rearrange the following to suit your stamping needs.
# it currently generates YYYYMMDDhhmmss
my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d",
$now[5]+1900, $now[4]+1, $now[3],
$now[2], $now[1], $now[0]);
# insert stamp into constant portion of file name.
# the constant portion of the name could be included
# in the sprintf() above.
my $fileName = "File$timeStamp.log";