Brad Cahoon wrote:
> Hi again,

Hello,

> I have lots of files ( see my first post to this group :) )
> they all have two lines simular to:
> 
>   Ref.: 005803/11-SY (T45)                        Total Amount        
> 685.00
>  Lead: ARDA/DILAN/MISS                             Total Paid        
> 685.00
> 
> I want to rename the files with "Lead"-"Ref".txt
> When I run the script I get an error :
> Use of uninitialized value in concatenation (.) or string at rename.pl
> line 15,
> <DATA> line 70.
> 
> and then the same error over 10,000 times before I pull the plug (ctl-c)
> 
> sorry to be a pain but I cannot see the error of my ways.
> 
> My script:
> 
> #!  /usr/bin/perl
> use strict;
> use warnings;
> 
> my $newname;
> my $end= '.txt';
> my $refno;
> my $lead;
> 
> 
> my $newfile;
> my @files = <c:\\test\\*.txt>;
> my $MyWrkLoc = q[c:\\test\\test1\\];
> 
> foreach $arg(@files){

Because of the strict pragma you have to declare all your variables so that
should be:

foreach my $arg ( @files ) {


>     open DATA, "$arg";

You should *always* verify that the file opened correctly.

perldoc -q quoting

>     opensub();
>     $newname = "$lead-$refno$end";
>     rename $arg, "$MyWrkLoc$newname";

You should *always* verify that the file was renamed correctly.

Because you are running this on Windows you may not be able to rename the file
while it is still open so you may have to close it first.  (AFAIK, I don't use
Windows myself.)


> }
> 
> sub opensub {
>     while (<DATA>) {
>         chomp($_);
>          if ($_ =~ m/^Ref.:(\S)/i) {    # get Reference Number

According to your sample data you have whitespace before and after the string
'Ref.:' and to match a literal . you have to escape it.  You are capturing a
single character but it looks like you may want to capture more than one?

          if ( /^\s*Ref\.:\s*(\S+)/i ) {    # get Reference Number


>         $refno=$1;
>         $refno=~ s/[\/|-]/_/;

You are substituting a '/' or a '|' or a '-' character with a '_' character
but only the first one.  If you have more than one substitution you need to
use the /g option or use the transliteration operator instead:

         $refno =~ tr=/|-=_=;


> print $refno;
>         }
>        
>         if ($_ =~ m/^Lead:(\S)/i) {    # get Lead Name
>         $lead=$1;
>         $lead=~ s/[\/|-]/_/;
> print $lead;
>         }
>     }

You could make this more efficient if you return (and close the filehandle) as
soon as you have the data that you require.


> }
> 
> ## the two print statements are just debuging pls ignore ##




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to