Help on For Loop variation

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.
 
B

Ben Morrow

Quoth "Deepu said:
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

use warnings;

nowadays.
use strict;

my @array = qw(2006_12_01 2006_12_02 2006_12_03 2006_12_04 2006_12_05);
<snip sorting by hand with a for loop>

Don't reinvent wheels. You seem to be storing dates as "YYYY_MM_DD",
which means they will sort correctly as text; so the last three such
dates from @array is

my @arrayOut = ( sort @array )[-3..-1];

and you can use a variable instead like this

my $count = 3;
my @arrayOut = ( sort @array )[ -$count .. -1 ];

Ben
 
J

J. Gleixner

Deepu said:
Hi All,

I have a code which generates the latest 3 directories based on the
date.

No need for all that code, sort will handle your formatted date just fine.

my @array = qw(2006_12_01 2006_12_02 2006_12_03 2006_12_04 2006_12_05);

print join( ' ', ( sort @array ) [-1, -2, -3] ), "\n";

2006_12_05 2006_12_04 2006_12_03



In this code i have a for loop which decides how many directories
 
D

Deepu

Use

use warnings;

nowadays.

Don't reinvent wheels. You seem to be storing dates as "YYYY_MM_DD",
which means they will sort correctly as text; so the last three such
dates from @array is

my @arrayOut = ( sort @array )[-3..-1];

and you can use a variable instead like this

my $count = 3;
my @arrayOut = ( sort @array )[ -$count .. -1 ];
I tried this part of the code(with @array=(2006_12_04, 2006_11_09,
2006_12_05, 2006_12_08,2006_12_20) and the @arrayOut will have:
$arrayOut[0] = 2006_12_05
$arrayOut[1] = 2006_12_08
$arrayOut[2] = 2006_12_20

How can i change it such that i store in the reverse order like:
$arrayOut[0] = 2006_12_20
$arrayOut[1] = 2006_12_08
$arrayOut[2] = 2006_12_05

Also if i have directory name something like 2006_12_20_TEST,
EX_2006_11_12, ...
How can i ignore these kind of names in the array?

Thanks for the help.
 
J

John W. Krahn

Deepu said:
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

my @arrayOut = ( reverse sort @array )[ 0 .. 2 ];

Or:

my @arrayOut = reverse +( sort @array )[ -3 .. -1 ];


And with a variable:

my $how_many_directories_to_store = 3;

my @arrayOut = reverse +( sort @array )[ -$how_many_directories_to_store .. -1 ];




John
 
D

Deepu

John said:
my @arrayOut = ( reverse sort @array )[ 0 .. 2 ];

Or:

my @arrayOut = reverse +( sort @array )[ -3 .. -1 ];


And with a variable:

my $how_many_directories_to_store = 3;

my @arrayOut = reverse +( sort @array )[ -$how_many_directories_to_store .. -1 ];

Thanks for the reply..i had one other question. if i have an array with
elements like:

@array = qw(2006_12_20, 2006_12_20_TEST, 2006_11_19, 2006_12_19);

Is it possible to ignore 2006_12_20_TEST?

I need in @arrayOut = (2006_12_20, 2006_12_19) when
$how_many_directories_to_store = 2.

Thanks for the help.
 
A

anno4000

Deepu said:
my @arrayOut = ( reverse sort @array )[ 0 .. 2 ];

Or:

my @arrayOut = reverse +( sort @array )[ -3 .. -1 ];


And with a variable:

my $how_many_directories_to_store = 3;

my @arrayOut = reverse +( sort @array )[
-$how_many_directories_to_store .. -1 ];

Thanks for the reply..i had one other question. if i have an array with
elements like:

@array = qw(2006_12_20, 2006_12_20_TEST, 2006_11_19, 2006_12_19);

Is it possible to ignore 2006_12_20_TEST?

Untested:

my @arrayOut = ( reverse sort grep /^\d{4}_\d{2}_\d{2}$/, @array )[ 0 .. 2 ];

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top