Home > Archive > PERL Beginners > June 2005 > assigning printf statement to an array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
assigning printf statement to an array
|
|
| DBSMITH@OhioHealth.com 2005-05-20, 8:55 pm |
| Here is my code, but I am not seeing the elements being populated as I say
show me the data in element 1????
Line 28 is not working after executing line 30.
Any ideas?
thank you,
derek
#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} =
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> my $hdir = qq(/heartlab/db studies/);
5> $^T=time;
6> our $now = time();
7> use constant ONE_DAY => 86400;
8> sub date_manip
9> {
10> my ($month, $day, $year) = (localtime)[4,3,5];
11> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
100));
12> }
13> my $dm = &date_manip;
14> sub date_manip1
15> {
16> my $days = (shift);
17> my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
[3..5];
18> sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
19> }
#my $dmm = &date_manip1(1);
#chomp $dm;
#chomp $dmm;
20> opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
21> my @a =();
22> my $e =0;
23> foreach my $file (sort readdir (DH))
24> {
25> next if $file eq "." or $file eq "..";
26> print "\n";
27> print "Days old of HL image files are:\t",$file,":\t";
28> $a[$e++] = printf ("%.03f", ( $now - ( stat(
"${hdir}${file}" ) )[9] ) / ONE_DAY );
#date_manip1($1);
29> }
30> print "\nARRAY ELEMENT 1\t",$a[1],"\n";
31> closedir (DH) or warn "unable to close DH: $hdir $!;"
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
| |
| Offer Kaye 2005-05-21, 8:56 am |
| On 5/21/05, DBSMITH@ohiohealth.com <DBSMITH@ohiohealth.com> wrote:
> Here is my code, but I am not seeing the elements being populated as I sa=
y
> show me the data in element 1????
> Line 28 is not working after executing line 30.
> Any ideas?
>=20
You probably want to use "sprintf" instead of "printf". Read "perldoc
-f printf" and "perldoc -f sprintf" from your command-line, or online:
http://perldoc.perl.org/functions/printf.html
http://perldoc.perl.org/functions/sprintf.html
HTH,
--=20
Offer Kaye
| |
| DBSMITH@OhioHealth.com 2005-05-23, 8:55 pm |
| 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);
}
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
| |
| John Doe 2005-05-24, 3:55 am |
| Am Montag, 23. Mai 2005 20.05 schrieb DBSMITH@OhioHealth.com:
> 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
| |
| displeaser 2005-05-24, 4:47 am |
| Hi,
you may be interested in this article: http://www.stonehenge.com/merlyn/UnixReview/col06.html
it's by the author/creator of the Schwartzian Transform.
D
ps there are lots of good Perl articles here & is well worth the time and effort to go through | |
| DBSMITH@OhioHealth.com 2005-05-24, 3:56 pm |
| 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.").
This is working but it not printing the calculation "days old" for each
filename??? 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
I will also follow up with a line by line question of the code.
thanks again!
derek
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
John Doe
<security.departm
ent@tele2.ch> To
beginners@perl.org
05/24/2005 01:34 cc
AM
Subject
Re: assigning printf statement to
an array
Am Montag, 23. Mai 2005 20.05 schrieb DBSMITH@OhioHealth.com:
> 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| John Doe 2005-05-24, 8:56 pm |
| Hi
Am Dienstag, 24. Mai 2005 18.05 schrieb DBSMITH@OhioHealth.com
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 DBSMITH@OhioHealth.com:
>
> this
>
>
> associative
>
>
> 100));
>
>
> )
>
>
> 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
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| DBSMITH@OhioHealth.com 2005-05-25, 3:57 am |
| 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
ent@tele2.ch> 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 DBSMITH@OhioHealth.com
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 DBSMITH@OhioHealth.com:
am[color=darkred]
>
> this
>
array/hash[color=darkred]
is[color=darkred]
>
> associative
>
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);[color=darkred]
>
> 100));
>
$!";[color=darkred]
)[9][color=darkred]
>
> )
>
>
> 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
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Jeff 'japhy' Pinyan 2005-05-25, 3:57 am |
| On May 24, DBSMITH@OhioHealth.com said:
> 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
> how does the perl compiler load this, from line 9 up or opendir down?
Well, Perl reads it left-to-right. It just happens that it's a bunch of
functions whose arguments are the return values of other functions. It's
the same as
print get_output(
get_old_ones(
make_age_filename(
exclude_dot_and_dotdot(
sort(
readdir(DIR)
)
)
)
)
);
where those functions do what you have above.
> And what is the correct way to read it from top down or bottom up?
Humans should probably read it from the bottom up. The raw data are the
arguments to the right-most function, and then it goes through many
filters until it is in its final form and printed.
> what is the purpose of an array reference in this code and what is it
> advantages?
The array reference is just a way of holding two pieces of data at once.
> 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?
The array reference is created in lines 4-7.
> 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?
That's the '.' operator, used for string concatenation.
$_->[1] . ": " . $_->[0] . "\n"
could just be written as
"$_->[1]: $_->[0]\n"
> so [1] contains the dir name and [0] contains the my days old
> calculation, correct?
Yes.
> how does map know to create this array b/c I thought map expects a list
> that is already created?
map() takes a list and returns a list. The elements of the list it
receives are filenames, the elements of the list it returns are array
REFERENCES.
> from the previous question... is this list the opendir? I guess am not
> totally grasping the map function.
Then you should read 'perldoc -f map'. map() is like a for loop that
returns a list.
@out = map { ...code... } @in;
is like:
@out = ();
for (@in) {
push @out, ...code...;
}
Here's a simple example:
@even_numbers = map { $_ * 2 } (1 .. 10);
That creates @even_numbers = (2, 4, 6, 8, 10, 12, 14, 16, 18, 20). The
same result can be gotten with a for loop:
@even_numbers = ();
for (1 .. 10) {
push @even_numbers, $_ * 2;
}
> in lines 5 and 6 $_ is the dir, but why is it needed at the very end at
> line 6 after the comma?
Because lines 4-7 are creating an array reference. The first element is
the age of the file, and the second element is the file, $_.
map { [ get_age($_), $_ ] } ...
The moral of the story is, you can do things all at once using map() and
grep(), or you can do them piecemeal with for loops:
> 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
Could be:
# line 9
my @all_files = sort readdir DH;
# line 8
my @files;
for (@all_files) { push @files, $_ if $_ ne "." and $_ ne ".." }
# lines 4-7
my @files_and_ages;
for (@files) {
push @files_and_ages, [
sprintf(...), $_
];
}
# line 3
my @old_files;
for (@files_and_ages) {
push @old_files, $_ if $_->[0] > 100;
}
# line 2
my @strings;
for (@old_files) {
push @strings, "$_->[1]: $_->[0]\n";
}
# line 1
print @strings;
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| John Doe 2005-05-25, 8:56 pm |
| Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:
[...]
Jeff, thanks a lot for taking over.
My explanation would not have been so understandable and with this fullness.
joe
| |
| Jeff 'japhy' Pinyan 2005-05-25, 8:56 pm |
| On May 25, John Doe said:
> Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:
>
> Jeff, thanks a lot for taking over.
> My explanation would not have been so understandable and with this fullness.
Glad to be of service.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| DBSMITH@OhioHealth.com 2005-05-26, 3:57 pm |
| DITTO!
THANKS Men!
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
John Doe
<security.departm
ent@tele2.ch> To
beginners@perl.org
05/25/2005 04:48 cc
PM
Subject
Re: assigning printf statement to
an array
Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:
[...]
Jeff, thanks a lot for taking over.
My explanation would not have been so understandable and with this
fullness.
joe
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| DBSMITH@OhioHealth.com 2005-06-08, 3:57 am |
| 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
DBSMITH@OhioHealt
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
ent@tele2.ch> 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 DBSMITH@OhioHealth.com
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 DBSMITH@OhioHealth.com:
am[color=darkred]
>
> this
>
array/hash[color=darkred]
is[color=darkred]
>
> associative
>
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);[color=darkred]
>
> 100));
>
$!";[color=darkred]
)[9][color=darkred]
>
> )
>
>
> 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
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
|
|
|
|
|