D
Deepu
Hi All,
I have a code which generates the latest 3 directories based on the
date. In this code i have a for loop which decides how many directories
i need to store in an array "arrayOut". Now i need to change the loop
in such a way that i should provide how many directories to store in
"arrayOut" using a variable. Can someone please help me on this.
I am basically trying to even store 1 directory in this array.
#!/usr/bin/perl -w
use strict;
my @array = qw(2006_12_01 2006_12_02 2006_12_03 2006_12_04 2006_12_05);
my @arrayOut = ();
# up to 3 directories
for (0..2)
{
# Start directory all zeros for comparison
my $latestDir = "0000_00_00";
# Perform the sorting operation for each directory in the array
foreach my $dir (@array)
{
my ($latestYear, $latestMonth, $latestDay) = split (/_/,
$latestDir);
my ($arrayYear, $arrayMonth, $arrayDay) = split (/_/, $dir);
# Compare latestYear and arrayYear, if true compare month and
day
if ($latestYear < $arrayYear)
{
$latestDir = $dir;
next;
} elsif ($latestYear == $arrayYear)
{
if ($latestMonth < $arrayMonth)
{
$latestDir = $dir;
next;
} elsif ($latestMonth == $arrayMonth)
{
if ($latestDay < $arrayDay)
{
$latestDir = $dir;
next;
}
}
}
}
# Add the element to the end of the array - arrRegrs
push (@arrayOut, $latestDir);
# delete the latestDir from the array and continue the operation
with the new array elements
for my $i (0..$#array)
{
if ($array[$i] eq $latestDir)
{
delete $array[$i];
}
}
}
print "@arrayOut\n";
Thanks for your time.
I have a code which generates the latest 3 directories based on the
date. In this code i have a for loop which decides how many directories
i need to store in an array "arrayOut". Now i need to change the loop
in such a way that i should provide how many directories to store in
"arrayOut" using a variable. Can someone please help me on this.
I am basically trying to even store 1 directory in this array.
#!/usr/bin/perl -w
use strict;
my @array = qw(2006_12_01 2006_12_02 2006_12_03 2006_12_04 2006_12_05);
my @arrayOut = ();
# up to 3 directories
for (0..2)
{
# Start directory all zeros for comparison
my $latestDir = "0000_00_00";
# Perform the sorting operation for each directory in the array
foreach my $dir (@array)
{
my ($latestYear, $latestMonth, $latestDay) = split (/_/,
$latestDir);
my ($arrayYear, $arrayMonth, $arrayDay) = split (/_/, $dir);
# Compare latestYear and arrayYear, if true compare month and
day
if ($latestYear < $arrayYear)
{
$latestDir = $dir;
next;
} elsif ($latestYear == $arrayYear)
{
if ($latestMonth < $arrayMonth)
{
$latestDir = $dir;
next;
} elsif ($latestMonth == $arrayMonth)
{
if ($latestDay < $arrayDay)
{
$latestDir = $dir;
next;
}
}
}
}
# Add the element to the end of the array - arrRegrs
push (@arrayOut, $latestDir);
# delete the latestDir from the array and continue the operation
with the new array elements
for my $i (0..$#array)
{
if ($array[$i] eq $latestDir)
{
delete $array[$i];
}
}
}
print "@arrayOut\n";
Thanks for your time.