Help on For Loop variation

Discussion in 'Perl Misc' started by Deepu, Dec 11, 2006.

  1. Deepu

    Deepu Guest

    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.
    Deepu, Dec 11, 2006
    #1
    1. Advertising

  2. Deepu

    Ben Morrow Guest

    Quoth "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

    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

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    Heraclitus
    Ben Morrow, Dec 11, 2006
    #2
    1. Advertising

  3. Deepu

    J. Gleixner Guest

    Deepu wrote:
    > 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
    > 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.
    J. Gleixner, Dec 11, 2006
    #3
  4. Deepu

    Deepu Guest


    > 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.
    Deepu, Dec 12, 2006
    #4
  5. Deepu wrote:
    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Dec 12, 2006
    #5
  6. Deepu

    Deepu Guest

    John W. Krahn wrote:
    > 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.
    Deepu, Dec 12, 2006
    #6
  7. Deepu

    -berlin.de Guest

    Deepu <> wrote in comp.lang.perl.misc:
    >
    > John W. Krahn wrote:
    > > 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
    -berlin.de, Dec 12, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alison Bowes
    Replies:
    0
    Views:
    463
    Alison Bowes
    Feb 18, 2005
  2. Erlend Andreas Garberg

    Travelling salesman variation in python

    Erlend Andreas Garberg, Apr 1, 2004, in forum: Python
    Replies:
    20
    Views:
    4,149
    G√ľnter Jantzen
    Apr 2, 2004
  3. j0mbolar

    opinions on logical OR variation

    j0mbolar, Sep 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    297
    Tim Rentsch
    Sep 29, 2004
  4. yanyo

    standard variation

    yanyo, Oct 6, 2005, in forum: C Programming
    Replies:
    1
    Views:
    940
    Alexei A. Frounze
    Oct 6, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    348
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page