On May 24, [EMAIL PROTECTED] 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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>