Re: looping questions
- From: gbacon@xxxxxxxxxx (Greg Bacon)
- Date: Fri, 28 Sep 2007 20:56:58 -0000
In article <1191010028.753764.76290@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
lerameur <lerameur@xxxxxxxxx> wrote:
: for my $hours ('00'..'23') {
: [...]
: $hours++;
: }
Your code is doing twice as much work as necessary to compute
the values of $hours:
1. Your for loop sets up a loop over '00' .. '23'
2. You increment $hours at the end of each iteration,
but the value is ignored.
You can remove the line with the increment.
: $file23 = glob("$timestamp2$hours*") ;
What if there's more than one match?
: if ($file23 == 1){
: system(`cp /input/fttr/traffic/$file23 /input/$Out_directory `);
: }
I don't see how the condition could ever be true because your
glob shouldn't match a file whose name is 1.
Perl's system operator wants a command to run, e.g.,
system("ls -l /etc/motd");
Backticks (``) also want a command. The value of a command
quoted with backticks is the output of running it. For example:
$ perl -e 'print `echo 1 + 2 | bc` * 4, "\n"'
12
You usually don't want to combine them as you've done because
that would run one command and then attempt to run its output
a second command, e.g.,
$ perl -e 'system `echo ls /`'
Consider the following improvements:
for my $hours ('00' .. '23') {
my $cmd = join " " => "cp",
"/input/fttr/traffic/$timestamp2$hours*",
"/input/$Out_directory";
system $cmd;
}
You could even condense your code to a single command:
my $base = "/input/fttr/traffic/$timestamp2";
system join " " => "cp",
map("$base$_*", '00' .. '23'),
"/input/$Out_directory";
Hope this helps,
Greg
--
Some cause happiness wherever they go; others whenever they go.
-- Oscar Wilde
.
- References:
- looping questions
- From: lerameur
- looping questions
- Prev by Date: Re: Sort and remove duplicates
- Next by Date: Re: looping questions
- Previous by thread: Re: looping questions
- Next by thread: Re: looping questions
- Index(es):
Relevant Pages
|
|