Please help on how to use the functionality of my date_manip1 subroutine to pass it the days old to get "the past date dd/mm/yy" as such:
ABDELFATTAH__FAITH_M702515438: 101 dd/mm/yy thank you, derek Here is my code: use strict; use warnings; $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log); my $hdir = qq(/heartlab/db studies/); my $fdir = qq(/fuji/original/RMH/); $^T=time; our $now = time(); use constant ONE_DAY => 86400; # sub date_manip1 # { # my $days = (shift); # my ($d, $m, $y) = (localtime($now - ($days * ONE_DAY))) [3..5]; # sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100)); # } #my $dmm = &date_manip1($0); #chomp $dm; #chomp $dmm; #sub secondsold #{ opendir (HDH, $hdir) or die "unable to open directory: $hdir $!"; my ($d, $m, $y) = (localtime($now - (map {$_->[0]} * ONE_DAY))) [3..5]; printf ("%02d/%02d/%04d", $m+1,$d,($y % 100)); grep {$_->[0] > 100} map { [ sprintf ("%.0f",($now - ( stat( "${hdir}$_" ) )[9] ) * ONE_DAY ), $_ ] } grep {$_ ne "." and $_ ne ".."} sort readdir HDH; #print @a,"\n"; closedir (HDH) or warn "unable to close HDH: $hdir $!"; # } ___END CODE___ ___BEGIN DATA___ Days old of HL image files are: 0_LEARY_ALANRA002848: 150 Days old of HL image files are: ABDELFATTAH__FAITH_M702515438: 101 [EMAIL PROTECTED] h.com To 05/24/2005 09:18 beginners@perl.org PM cc Subject Re: assigning printf statement to an array operdir ..... 1> print 2> map {$_->[1].": ".$_->[0]."\n"} # create entity output 3> grep {$_->[0] > 100} # exclude age <= 100 4> map { [ 5> sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ), 6> $_ 7> ] } # create aref [age, filename] 8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted 9> sort readdir DH; # get all files closedir ... In the block of code : how does the perl compiler load this, from line 9 up or opendir down? And what is the correct way to read it from top down or bottom up? what is the purpose of an array reference in this code and what is it advantages? on pg 253 of the camel book it states "if the next thing after the -> is a bracket, then the left operand ($_ ) is treated as a reference to an array to be subscripted on the right [0] or [1] . So is this array declared/created dynamically? How? on line 2 why is the period after [1] needed and what does it represent , string concatenation? If so why are multiple ones needed on line 2? so [1] contains the dir name and [0] contains the my days old calculation, correct? how does map know to create this array b/c I thought map expects a list that is already created? from the previous question... is this list the opendir? I guess am not totally grasping the map function. in lines 5 and 6 $_ is the dir, but why is it needed at the very end at line 6 after the comma? to the end, output of days old > 100 I am now trying to use my date_manip1 subroutine to pass it the days old to get "the past date dd/mm/yy" as such: line 3 turns into &date_manip1(grep {$_->[0] > 100} ) so I have output as : ABDELFATTAH__FAITH_M702515438: 101 dd/mm/yy I am on the right track? thank you, derek John Doe <security.departm [EMAIL PROTECTED]> To beginners@perl.org 05/24/2005 03:27 cc PM Subject Re: assigning printf statement to an array Hi Am Dienstag, 24. Mai 2005 18.05 schrieb [EMAIL PROTECTED] with funny quoting: [John:] > my @a= > map {$hdir.$_->[1]} # select filename > grep {$_->[0] > 100} # exclude age <= 100 > map { [ > sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ), > $_ > ] } # create aref [age, filename] > grep {$_ ne "." and $_ ne ".."} # exclude unwanted > sort readdir DH; # get all files > > > The map-grep-map-construct is quite common for problems like yours > ("But I cannot use $_ b/c $_ is the dir name."). [Derek:] > This is working but it not printing the calculation "days old" for each > filename??? No, obviously not... I wanted to demonstrate the principle where some kind of additional attributes (here: age) of a list of entities (files) is created on the fly and kept together (in an aref), and taken as base for further treatment (selection). [ Hope this english is understandable :-) ] > I want to print the aref. > I verified that it is indeed only printing those files over 100 days old, > but I need output such as, > > ABDELFATTAH__FAITH_M702515438: 101 > > as opposed to > > ABDELFATTAH__FAITH_M702515438 Ok, then some minimal changes in the first two lines are necessary (although there are other possibilities to do what you want; my proposal is possibliy not the most performant, but splits the problem into smaller parts that can be chaned) [also untested...] print map {$_->[1].": ".$_->[0]."\n"} # create entity output grep {$_->[0] > 100} # exclude age <= 100 map { [ sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ), $_ ] } # create aref [age, filename] grep {$_ ne "." and $_ ne ".."} # exclude unwanted sort readdir DH; # get all files > I will also follow up with a line by line question of the code. Just go on, questions are half of the answers :-) joe [...] [maybe I should have snipped the rest?] > Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]: > > Hi all..... > > > > This is a continuation from my original question, but with a twist. I am > > now using sprintf to assign to an array, but I only want to assign to > > this > > > array of I find a number over 100 from my calculation. In the array/hash > > also needs to be the directory name. Here is my code and the sample > > output: > > So in the end my goal is to store the data in this array if the number is > > over 100 and if it is over 100 store the directory name and its > > associative > > > "days old calc." > > I have tried my @a =(); my $e =0; > > $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) / > > ONE_DAY ) if ??? > 100; > > > > But I cannot use $_ b/c $_ is the dir name. > > > > thank you, > > > > derek > > > > > > > > > > use strict; > > use warnings; > > $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log); > > > > my $hdir = qq(/heartlab/db studies/); > > #my $fdirs = > > qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj > > i/clinical/RMH/); > > $^T=time; > > our $now = time(); > > use constant ONE_DAY => 86400; > > > > > > sub date_manip > > { > > my ($month, $day, $year) = (localtime)[4,3,5]; > > sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % > > 100)); > > > } > > > > my $dm = &date_manip; > > > > sub date_manip1 > > { > > my $days = (shift); > > my ($d, $m, $y) = (localtime($now - $days * ONE_DAY)) > > [3..5]; > > sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100)); > > } > > > > #my $dmm = &date_manip1(1); > > #chomp $dm; > > #chomp $dmm; > > > > > > > > opendir (DH, $hdir) or die "unable to open directory: $hdir $!"; > > > > my @a =(); > > my $e =0; > > foreach my $file (sort readdir (DH)) > > { > > next if $file eq "." or $file eq ".."; > > print "\n"; > > print "Days old of HL image files are:\t",$file,":\t"; > > #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9] > > ) > > > / ONE_DAY ); > > $a[$e++] = sprintf ("%.0f",( $now - ( stat( > > "${hdir}${file}" ) )[9] ) / ONE_DAY ); > > #date_manip1($1); > > } > > instead of the foreach loop you could use a single statement [untested]: > (read from bottom to top) > > my @a= > map {$hdir.$_->[1]} # select filename > grep {$_->[0] > 100} # exclude age <= 100 > map { [ > sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ), > $_ > ] } # create aref [age, filename] > grep {$_ ne "." and $_ ne ".."} # exclude unwanted > sort readdir DH; # get all files > > > The map-grep-map-construct is quite common for problems like yours > ("But I cannot use $_ b/c $_ is the dir name."). > > (Schwarz'sche Transformation in german). > > > hth, joe > > > closedir (DH) or warn "unable to close DH: $hdir $!;" > > > > ___END CODE___ > > > > ___BEGIN_DATA___ > > > > Days old of HL image files are: 0_LEARY_ALANRA002848: 68 > > Days old of HL image files are: AARON_YAZAWA_CHRISTEHD006871: 48 > > Days old of HL image files are: AARON_YAZAWA_CHRISTERF015357: 64 > > Days old of HL image files are: ABBOTT_ANNA_MAERF014496: 49 > > Days old of HL image files are: ABBOTT_CONSTANCEMK003126: 69 > > Days old of HL image files are: ABBOTT_DONALDHE011252: 67 > > Days old of HL image files are: ABBOTT_FLORENCE068148_15857: 20 > > Days old of HL image files are: ABBOTT_HARRYRA002652: 68 > > Days old of HL image files are: ABBOTT_THOMASMKOOOO61: 66 > > Days old of HL image files are: ABBOTT__FLORENCEHC008241: 20 > > Days old of HL image files are: ABBRUZZESE_BETTYMK004528: 78 > > Days old of HL image files are: ABBRUZZESE_BETTYML002169: 80 > > Days old of HL image files are: ABDALLA__PATRICIAHA014365: 56 > > Days old of HL image files are: ABDELFATTAH_FAITH0702515438: 98 > > Days old of HL image files are: ABDELFATTAH__FAITH_M702515438: 101 > > Days old of HL image files are: ABDULLAHI__ABDIRAHMAMJ003339: 62 > > Days old of HL image files are: ABELL_MARKRF015325: 66 > > Days old of HL image files are: ABEL_HUBERTMJ003654: 21 > > Days old of HL image files are: ABEL_RICHARDMK003922: 82 > > Days old of HL image files are: ABEL_STEVENHD007640: 75 > > Days old of HL image files are: ABEL_STEVENRA003253: 75 > > Days old of HL image files are: ABEL__LARRYRF013486: 73 > > > > > > > > > > Derek B. Smith > > OhioHealth IT > > UNIX / TSM / EDM Teams > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>