Ma has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a date in this format "Oct 31 08:30". I would like to convert into Day and Month (like Day=31 Month=10) I have tried this and did not work:
my $file_to_parse = "Date.csv "; open(FH, '<', $file_to_parse) or error("cannot open file ($!)"); while (<FH>) { chomp; my ($date, $file_name) = split(","); $date =~ s@\r|\n@@g; # get rid of trailing newlines, however forma +tted my ($month, $day, $year) = split(m@/@, $date); print "substr $month, 1,2\n"; last; } close (FH);

Replies are listed 'Best First'.
Re: Convert date into day and month
by kcott (Archbishop) on Nov 01, 2013 at 13:06 UTC

    G'day Ma,

    I find the builtin module Time::Piece is particularly good at handling this type of task:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; my $date_string = 'Oct 31 08:30'; my $t = Time::Piece->strptime($date_string, '%b %d %H:%M'); print 'Day=', $t->mday, ' Month=', $t->mon;

    Output (exactly as requested):

    Day=31 Month=10

    -- Ken

Re: Convert date into day and month
by Happy-the-monk (Canon) on Nov 01, 2013 at 13:02 UTC

    my ($date, $file_name) = split(","); ... my ($month, $day, $year) = split(m@/@, $date);

    You should check $date's contents to make sure that you get it and what you've got.

    You write your date has information separated by whitespace yet you match on a slash: / - that can't work, can it?

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Convert date into day and month
by Lotus1 (Vicar) on Nov 01, 2013 at 13:09 UTC
    while (<FH>) { chomp; my ($date, $file_name) = split(","); $date =~ s@\r|\n@@g; # get rid of trailing newlines, however forma +tted

    When you read line by line with while (<FH>) you don't need to worry about extra newlines. You chomp()'ed the one off. If you gave us some example lines from your Date.csv file it would be easier to see what you are trying to do. Better yet, add the lines to your perl file after __DATA__ and use while (<DATA>)so we can quickly download your code and test.

Re: Convert date into day and month
by fishmonger (Chaplain) on Nov 01, 2013 at 13:40 UTC

    You could use a hash that maps the month name to a number and then use a regex to change the date string.

    Example:
    #!/usr/bin/perl use strict; use warnings; my %mon2num = ( Sept => 9, Oct => 10, Nov => 11, # etc ); while (<DATA>) { chomp; my ($date, $file_name) = split(/,/); $date =~ s/(\S+)\s(\S+)\s(\S+)/$2 $mon2num{$1} $3/; print "$date\n"; } __DATA__ Oct 31 08:30,file1 Nov 25 17:30,file2 Sept 09 12:00,file3
Re: Convert date into day and month
by Laurent_R (Canon) on Nov 01, 2013 at 13:53 UTC

    In addition to what has been said earlier about the separators in your dates, I would suggest that, if the line that you are reading consist of a date and a filename separated by a comma, as suggested by this line of code:

    my ($date, $file_name) = split(",");
    then the following line:
    $date =~ s@\r|\n@@g; # get rid of trailing newlines, however formatted
    is probably useless, since the date is not anywhere near the end of line.

    For the rest, the best would be that you supply a short data sample.

      Thank you everyone, I got so many good replied. The Date.csv file is pasted below. What am I trying to achieve. I need to read just the first line, actually just the date and find out the date, month, and then year. Then I need to create the folder using something like:  system("md $Month$Day$Year"); I need to read the file just for reading the first line, get the day, month, and year and then create a folder
      Oct 31 08:30,/home/imanager/cppprod/AR_LM.csv Oct 31 07:32,/home/imanager/cppprod/AddressDevCHUBldgProj.csv Oct 31 08:06,/home/imanager/cppprod/AddressDevCHUBldgProj_LM.csv Oct 31 07:44,/home/imanager/cppprod/BalDue.csv Oct 31 08:07,/home/imanager/cppprod/BalDue_LM.csv Oct 31 07:50,/home/imanager/cppprod/Charges.csv Oct 31 08:10,/home/imanager/cppprod/Charges_LM.csv Oct 31 08:45,/home/imanager/cppprod/Dem_A_C.csv Oct 31 07:51,/home/imanager/cppprod/MonthlyEviction.csv Oct 31 08:11,/home/imanager/cppprod/MonthlyEviction_LM.csv

        Using kcott's Time::Piece built-in module suggestion, consider the following:

        use strict; use warnings; use Time::Piece; <> =~ /([^,]+)/ and my $dir = Time::Piece->strptime( "$1 2013", '%b %d %H:%M %Y' )-> +mdy('') or die "Unable to capture date string."; if ( !-e $dir ) { mkdir $dir or die "Unable to create directory $dir: $!"; print "Created dir: $dir\n"; } else { print "Directory $dir already exists.\n"; }

        Usage: perl inFile

        Since the date doesn't contain the year, that's hard coded. The in-line <> notation (short for <ARGV>) gets the first line of the file sent to the script. Next, a regex is used to capture that first line's date information (held in $1), and the date/year information is used by Time::Piece to return a 'mmddyyyy' string for the directory name. Finally, mkdir is used to create the directory if it doesn't already exist ( !-e $dir ).

        Hope this helps!

Re: Convert date into day and month
by Laurent_R (Canon) on Nov 01, 2013 at 16:15 UTC

    Hi,

    try this:

    use strict; use warnings; my $year = 2013; # hard coded as not in the data my %months = (Jan => "01", Feb => "02", Mar => "03", Other => "...4 5 +6 7 8 9", Oct => 10, Nov => 11, Dec => 12); my $line = <DATA>; my ($date, $rest) = split /,/, $line; my ($mon, $day) = split / /, $date; my $month = $months{$mon}; my $new_date = "$month$day$year"; print $new_date, "\n"; __DATA__ Oct 31 08:30,/home/imanager/cppprod/AR_LM.csv Oct 31 07:32,/home/imanager/cppprod/AddressDevCHUBldgProj.csv Oct 31 08:06,/home/imanager/cppprod/AddressDevCHUBldgProj_LM.csv Oct 31 07:44,/home/imanager/cppprod/BalDue.csv Oct 31 08:07,/home/imanager/cppprod/BalDue_LM.csv Oct 31 07:50,/home/imanager/cppprod/Charges.csv Oct 31 08:10,/home/imanager/cppprod/Charges_LM.csv Oct 31 08:45,/home/imanager/cppprod/Dem_A_C.csv
    This outputs this:
    $ perl dates.pl 10312013
    I would suggest that you use the mkdir Perl internal function, rather that a system call. Something like this:
    mkdir $new_date or die "could not create directory $new_date $!";

      Thank you. I appreciate very much. I have now everything needed to complete the script. I found the members of this community very very helping and knowledgeable.
Re: Convert date into day and month
by Anonymous Monk on Nov 02, 2013 at 03:17 UTC

    Hi,

    Just for TMTOWTDI, once you have your line and have set up the hash of month names/ numbers, you could also

    $mname = substr( $line, 0, 3 ); $mon = $mhash{$mname}; $day = sunstr( $line, 4, 2 );

    ( untested ) or something similar.

    J.C.