Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Appropriately assigning times to variables

by pKai (Priest)
on May 02, 2007 at 12:21 UTC ( #613158=note: print w/replies, xml ) Need Help??


in reply to Appropriately assigning times to variables

Not exactly answering your question maybe…
…but since your time string is perfect for pulling the time comparison into the domain of string comparison…

use strict; use warnings; chomp(my @directories_time_tagged = <DATA>); for my $directory_name ( @directories_time_tagged ) { next unless $directory_name =~ /(\d\d\d\d_\d\d_\d\d_\d\d)/; # Year_ +month_day_hour my $date_time = $1; my ($year, $month, $day, $hour) = split /_/, $date_time; my ($y_now, $m_now, $d_now, $h_now) = (localtime)[5,4,3,2]; my $now = sprintf('%d_%02d_%02d_%02d', $y_now+1900, $m_now+1, $d_now +, $h_now); if ($date_time le $now ) { # If the date has been passed print "\nconditions have been met for $directory_name\n"; } } __DATA__ foo_2006_03_02_14_verzeichnis bar_2007_05_02_02_directory baz_2007_05_03_22_dossier

Replies are listed 'Best First'.
Re^2: Appropriately assigning times to variables
by graff (Chancellor) on May 02, 2007 at 23:06 UTC
    Just a nitpick: I don't think you need to reassign a value to $now on every iteration of the loop.
Re^2: Appropriately assigning times to variables
by Win (Novice) on May 02, 2007 at 14:58 UTC
    I like this code because it takes out the need for a module. The wider context for the code you have given me is:
    my (@priority_numbers_in_order, @directories_to_transport) = Search_di +rectories ($input_directory_C, $node); my $count = 0; for (@directories_to_transport){ my $priority_number = $priority_numbers_in_order[$count]; my $specific_recipient_directory = "$recipient_directory"."$priority +_number"; $dir = $_; opendir(DIR, "$dir") or die $!; my $file_name; while (<DIR>){ $file_name = $_; } my $dir_and_filename = $dir.$file_name; rename "$dir_and_filename", "C:$specific_recipient_directory/$file_n +ame" or warn $!; $count++; } sub Search_directories { my ($input_directory_CC, $node) = @_; my $directory_of_interest = "$input_directory_CC"; opendir (DIRY, "$directory_of_interest") or die "Error opening direc +tory : $!"; my @directories; while ($_ = (readdir DIRY)) { print $_, "\n"; if (-d "$input_directory_CC/$_") { print "This is a dir $_ \n"; if ($_ =~ /\d$/){ print "This directory is a number: $_"; push (@directories, $_); } } } close DIRY; print "\n\&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n".$input_directory_CC.@dire +ctories."\n\n"; for my $dir_num (@directories) { my @direcories_to_transport; print "\nforeach of the direcotories : $dir_num\n"; my $input_dir = $input_directory_CC.$dir_num; print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>$input_dir"; opendir DIRY, $input_dir or die "Error opening directory $input_ +dir - $!"; my @directories_time_tagged = grep /\d*$/, readdir DIRY; closedir DIRY; chomp(my @directories_time_tagged = <DATA>); for my $directory_name ( @directories_time_tagged ) { next unless $directory_name =~ /(\d\d\d\d_\d\d_\d\d_\d\d)/; # Yea +r_month_day_hour my $date_time = $1; my ($year, $month, $day, $hour) = split /_/, $date_time; my ($y_now, $m_now, $d_now, $h_now) = (localtime)[5,4,3,2]; my $now = sprintf('%d_%02d_%02d_%02d', $y_now+1900, $m_now+1, $d_n +ow, $h_now); if ($date_time le $now ) { # If the date has been passed print "\nconditions have been met for $directory_name\n"; my $directory_to_move = $input_directory_CC.$dir_num..$directory +_name; push (@direcories_to_transport, $directory_to_move); push (@priority_numbers_in_order, $dir_num); } } } return @priority_numbers_in_order, @direcories_to_transport; }
    I've not tested it though.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://613158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2021-04-14 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?