You can use regular expressions to help match your data line various bits and pieces and do away with the multiple splits. Using regular expressions can also verify your line format. Do you have a 3 digit month? Do you have three dates? It's always a good idea to verify your input:
#! /usr/bin/env perl
#
use strict;
use warnings;
use feature qw(say);
my $date_re = qr(
^(?<month1>\d{1,2})/
(?<day1>\d{1,2})/
(?<year1>\d{2,4})
\| # Separator between date1 and date2
(?<month2>\d{1,2})/
(?<day2>\d{1,2})/
(?<year2>\d{2,4})
\| # Separator between date2 and date3
(?<month3>\d{1,2})/
(?<day3>\d{1,2})/
(?<year3>\d{2,4})
\| # Separator between date3 and name
(?<name>.*)
)x;
while ( my $line = <DATA> ) {
my @array;
if ( not @array = $line =~ m^$date_re^ ) {
say "Something's wrong";
}
else {
say "First Date: Year = $+{year1} Month = $+{month1} Day = $+{day1}";
say "Second Date: Year = $+{year2} Month = $+{month2} Day = $+{day2}";
say "Third Date: Year = $+{year3} Month = $+{month3} Day = $+{day3}";
say "Name = $+{name}";
}
}
__DATA__
12/23/2014|2/20/1995|3/25/1905|josh
Running this program prints out:
First Date: Year = 2014 Month = 12 Day = 23
Second Date: Year = 1995 Month = 2 Day = 20
Third Date: Year = 1905 Month = 3 Day = 25
Name = josh
This is using some advanced features of regular expressions:
qr/.../ can be used to define regular expressions. Since you have slashes in the regular expression, I decided to use parentheses to delimit my regular expression, so it's qr(...).
The )x at the end means I can use white space to make my regular expression easier to understand. For example, I broke out each of the dates onto three lines (month, day, year).
(?<name>...) names your capture group which makes it easier to refer back to a particular capture group. I can use the %+ hash to recall my capture groups. For example (?<month1>\d{1,2}) says that I expect a 1 to two digit month. I store this in the capture group month1, and I can refer back to this by using $+{month1}.
One of the nice things about using named capture groups is that it documents what you're attempting to capture.
The {M,N} is a repeat. I expect the previous regular expression to happen from M to N times. \d{1,2} means I'm expecting one or two digits.
/giwhere it's matching anchored numbers and brackets around your regex hyphen on your@datessplit. And most of yourelses don't seem to be doing anything useful at all.$dates[0] = sprintf("%02d", $dates[0]);, and ditch the if and regex. That said, the comment about/giwas that you need neither case insensitivity or multiple matches when you're matching numerical digits with start and end anchors./gswitch to match one item, and the/ito ignore case doesn't affect a regex that only matches numbers. They look like inherited cruft.