Re: looping questions



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
.



Relevant Pages

  • Re: Have perl increment a number that shows up before a delimiter
    ... > introduce this on the command line and have the script increment this ... There is no newline if you have entered the filename on the command ...
    (comp.lang.perl)
  • Re: dublicate report
    ... Question is how do you assign record number (Auto Number or increment of top ... Set itmx = YourListView.SelectedItem ... ListView which then can be selected to open report or record with command ...
    (microsoft.public.vb.database.dao)
  • Re: Counting, command button and programming in VB
    ... After that, left click anywhere in the Excel window, ... "Alan" wrote in message ... Sub Increment() ... command button which is the second one down on the right, ...
    (microsoft.public.excel.misc)
  • Re: looping questions
    ... You can remove the line with the increment. ... Perl's system operator wants a command to run, e.g., ... Backticks also want a command. ...
    (comp.lang.perl.misc)
  • Re: backtick and system return different exit codes
    ... > backticks cause it is returning the correct output. ... > I am not really interested in knowing the exact error code.i ... > want to know whether the command executed successfully. ...
    (comp.lang.perl.misc)