Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm not too good with Perl, so a problem like this stumbles me. I have a vision on how to do this but was wondering if there would be any other easier ways. I'm pretty sure there is, so I wish to seek wisdom from fellow monks here.

I have a directory called "images". Here is a example of what image files would be in this directory:
2005-03-05.jpg <b>2005-04-25.jpg 2005-04-27.jpg 2005-04-29.jpg 2005-05-01.jpg 2005-05-05.jpg</b> 2005-02-10.jpg
Now I have a script that displays an image from this directory according to the current date and the date range that was inputted inside a database.

Lets say the date range was 2005-04-25 to 2005-05-05, and todays date was 2005-04-28, then that means I want it to display the image file named 2005-04-27.jpg. If todays date was 2005-04-29, then it will display the image file named 2005-04-29.jpg instead.

This image display is basically an automation display of a image file according to the current date and whether its in the date range specified.

The code I'm envisioning is a to create an array of values of image file names according to the date range specified. Then do a foreach statement on that array to check which image file to display.

Am I on the right track?

Thanks,
Elrold

Replies are listed 'Best First'.
Re: Displaying a Image based on date range
by davidrw (Prior) on Jul 13, 2005 at 20:35 UTC
    Am i correct in summarizing the logic as you want to include the image of latest date that is on or before the given date? I think you have the general idea, though no need to read in an array .. just loop backwards with a date module (Date::Calc or any of the others) and check if that file exists. Something like (warning -- untested):
    use Date::Calc qw/ Today Delta_Days Add_Delta_Days /; my ($start_date, $end_date) = ('2005-04-25', '2005-05-05'); my @target_date = Today(); my @start_date = split /-/, $start_date; my @end_date = split /-/, $end_date; die "not in range" unless Delta_Days(@start_date, @target_date)>=0 && +Delta_Days(@target_date, @end_date)>=0; die "bad order" unless Delta_Days(@start_date,@end_date)>=0; my @d = @target_date; my $filename = ''; while( Delta_Days(@start_date,@d)>=0 ){ my $f = sprintf "%04d-%02d-%02d.jpg", @d; if( -f $f ){ $filename = $f; last; } @d = Add_Delta_Days(@d, -1); } print "File: $filename";
Re: Displaying a Image based on date range
by kirbyk (Friar) on Jul 13, 2005 at 20:47 UTC
    I think you're on a track that will work. There are a lot of approaches.

    I'd probably not load up all the files into an array - there might be a lot of them someday, and it's easy to check if a file exists.

    So, in pseudocode (since you're asking for approach), something like:

    use Date::Calc; # For date math my $day = some formatting to localtime... return if $day > $end_date; while ( $day > $start_date ) { # you'll have to either keep these in Y +YYYMMDD format, or do some more complicated comparison logic my $hopeful_filename = "images/$today"; if (-e $hopeful_filename) { return $hopeful_filename; } $day = Add_Delta_Days(...); # Subtract one from the day }
    That should get you on the write path. perldoc Date::Calc for the exact syntact for Add_Delta_Days.

    -- Kirby, WhitePages.com

Re: Displaying a Image based on date range
by tlm (Prior) on Jul 14, 2005 at 00:39 UTC

    Lets say the date range was 2005-04-25 to 2005-05-05, and todays date was 2005-04-28, then that means I want it to display the image file named 2005-04-27.jpg. If todays date was 2005-04-29, then it will display the image file named 2005-04-29.jpg instead.

    ...

    Am I on the right track?

    IMO, no, because it is not clear to me that you have fully specified the problem. Examples are essentially anecdotes; they are handy in some situations but they're not good enough to specify a task. Forget about programming for a second and imagine that you have hired a very expensive programmer to do this for you, and that you have to specify for him/her exactly what you want. Any mis-specification will end up costing you megabucks. Now, with this scenario in mind: what do you want to do?

    Once you know exactly what you want you can hope to write a program to do it.

    Of course, this is easier said than done, and often the process of programming significantly influences one's idea of what one wants, but still I think it is essential to start out with as clear and unambiguous specification of the task at hand as possible before one starts even thinking about programming it.

    the lowliest monk

Re: Displaying a Image based on date range
by anonymized user 468275 (Curate) on Jul 14, 2005 at 15:58 UTC
    I would say there is no need to worry about date rollover, because the collating sequence for YYYYMMDD is already in the right order. Therefore:
    opendir my $dh, "path/images" or die $!; my $min = '2005-04-05'; my $max = '2005-05-05'; my $found = ''; for my $file ( grep !/^\./, readdir $dh ) { unless ( $file =~ /^((\d){4}\-(\d){2}-(\d){2}\.jpg)$/ ){ warn "Rejected filename $file"; next; if ( ( $file le $max ) and ( $file ge $min ) and ( $file gt $found + ) ) { $found = $file; } } closedir $dh; print "$found\n";

    One world, one people