Dermot Paikkos wrote:
> 
> Hi gurus,

Hello,

> I have a script that is causing me some trouble. I haven't pasted the whole script 
> here
> as it is a bit long and I can't submit any test data.
> 
> Basically the script copies and renames file. At the end I would like to print out a
> report of all the files it has renamed. These reports are used by another process 
> later
> and the reports have to be unique to each user.
> 
> At the start I glob a directory for tif files and these are added to the @tiffs 
> array. Later
> a subroutine loops through the array and prints the file names into the user's log.
> However I only get the first file name from the file glob, all the others seem
> disappear. I have printed debug messages about the size of the array but it always
> has a length of 1 by the time it is in the sub routine.
> 
> Should I be using a array reference instead? It so can anyone explain how to use it?
> My initial attempts at using references has not been v. successful.
> 
> Thanx in advance.
> Dp.
> 
> ...snip
> ...snip
> while (defined(my $i = <$inpath/*.tif>)) {
>         chomp($i);

You don't need to chomp, there is no newline there.


>         my $n = lc($i);
>         my $name = basename($n);
>         print "added $name to array\n";
>         push(@tiffs,$name);
>         my $bar = @tiffs;
>         ++$in;
>         print "Array is now $bar big\n";
> ...
> ...
> ..
> }

You can simplify that a bit:

my @tiffs = map lc basename( $_ ), <$inpath/*.tif>;
print 'Array is now ' . @tiffs . " big\n";


Or:

opendir my $dh, $inpath or die "Cannot open $inpath: $!";
my @tiffs = map lc, grep /\.tif$/, readdir $dh;
closedir $dh;
print 'Array is now ' . @tiffs . " big\n";


> #  Create log
> scanlog($log_path,@tiffs);
> 
> sub scanlog {
> 
>  my $log_path = shift;
>  my (@tiffs) = shift;

shift() returns the first element of an array so you are only getting
one file name in @tiffs.  Also an array forces a list context on the
assignment so the parenthesis are not required.

my @tiffs = @_;


>  my $jpg;

You should declare this in the scope (foreach loop) where it is used.


>  print "\n$log_path\n";
> 
>  my $user_file = new Logfile::Rotate(
>         File => "$log_path",
                  ^         ^
You don't have to quote variables.

          File => $log_path,


>         Count => 20,
>         Gzip => "no",
>         );
> 
>  $user_file->rotate();
> 
> print "opening $log_path\n";
> open(SCAN,">$log_path") || die "Can't open $log_path: $!\n";
> 
> my $bar = @tiffs;
> print "Starting loop with [EMAIL PROTECTED] = $tiffs[0] next is $tiffs[1]\n";
> # $tiffs[1] prints undef
> 
> print "Array is $bar size\n";
> 
> foreach $jpg (@tiffs) {
          ^^^^
Declare the $jpg variable here.

foreach my $jpg ( @tiffs ) {


> # Put a carriage return in for MS WIN readers.
>         my $grr = "$jpg"."\015";
>         print "$jpg\n";

perl should print the correct characters for "\n" on Windows, you
shouldn't have to print "\015" yourself.


>         print SCAN "$grr\n";
>         }
> 
> close(SCAN);
> 
> }


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to