in reply to Convert date into day and month

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.

Replies are listed 'Best First'.
Re^2: Convert date into day and month
by Ma (Novice) on Nov 01, 2013 at 14:35 UTC
    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!