in reply to Not able to ParseDate lines from a file

Ok. Maybe I wasnt able to explain the problem. Here it is point by point. I want a particular line from a file, which has the format "Date: DD:MM:YYYY HH:MM:SS", parse it to identify the date part and find the epoch time. Here is what I am trying to do: Lets take the example of the line as "Date: 02.07.2011 16:14:48"

* I read the full date from the file (including the "Date:" which is included in the line) and put it in a scalar variable $array

* Once I get the date in $array, I read the individual numbers. For example, to get the Year, I use the substr function to find the exact start of the year, till the exact end. Here, 22 is the starting point of the year and reading 8 units to read the entire Year. Strangely, it is calculating 8 units for 4 digits.

* I do the same for the others, reading the Month, the day, the Hours, Minutes and Seconds using the substr function. When I print these values, I get the correct output. For $year, I get the 4 digit year, for example 2011. For $month, I get the two digit month value, 07. For $date, I get the two digits, and so on for all others.

* These values are passed on to the DateTime->new function to get the epoch time. This generates an error saying I have entered an undefined value!

I have tried playing around with the scalars, and I am not able to identify where I am wrong.

Corion, I had to put  $year=substr $array[13],22,8 since that is what is giving me the full year (2011).

I even used the Function ParseDate(). When I directly give the ParseDate() a string, like this  ParseDate("02.07.2011 16:14:48");, things work great, but when try the script $time=substr $array[13],22,8; ParseDate($time); I get an error saying the input is undef.

Jethro, when I executed your code using the Dumper, I got the following output: $VAR1 = "\0002\0000\0001\0000"; Would really appreciate if you could explain.

Thanks guys for the help. I hope I have been able to explain my problem. Looking forward to some more help. Thanks

Replies are listed 'Best First'.
Re^2: Not able to ParseDate lines from a file
by Corion (Patriarch) on Feb 03, 2012 at 12:51 UTC

    Your prose description of what you think your code does sounds nice. But it does not relate at all to what your program is doing, otherwise your program would be working.

    Reduce your program to a small, self-contained example. Also post the input data. Maybe your input data is nothing at all like what you expect. The Data::Dumper output seems to confirm that. The unusual length of 8 bytes for what should be a 4-byte number confirms that. So investigate, reduce and debug your program and thus your problem.

Re^2: Not able to ParseDate lines from a file
by Khen1950fx (Canon) on Feb 03, 2012 at 13:40 UTC
    DateTime::Format::Natural can parse the string for you. You don't have to reinvent the wheel. Try this:
    #!/usr/bin/perl -l use strict; use warnings; use DateTime; use DateTime::Format::Natural; my $parser = DateTime::Format::Natural->new; my $extract_string = "Date: 02.07.2011 16:14:48"; my $date_string = $parser->extract_datetime($extract_string); my $dt = $parser->parse_datetime($date_string); my $epoch_time = $dt->epoch; print $epoch_time;
Re^2: Not able to ParseDate lines from a file
by jethro (Monsignor) on Feb 03, 2012 at 14:36 UTC

    The other answers should help you already with your problem, but if you are interested in the mystery of your $year number, read on.

    If you want to specify control characters or some other non-printable character codes in perl you can use special codes. For example to put a newline into strings you could use \n or hexadecimal \x0a or octal \013. What you see in your Dumper output is a zero byte, \000 aka \x00, followed by \x32 which is (in ASCII or UTF-8) a "2" character followed by \x00 again.... It seems to be a 16-bit character format. Printing this will look like "2010" because \x00 doesn't have a character icon, but for perl it is not a string it can convert to numbers.