Patricia E Gorden-Ozgul wrote:
> 
> I need to construct a working printf statement where each field in a given
> record would print at a specified position.
> 
> Each record may or may not contain any particular field but will always
> begin with .VENDOR.LIBRARY. and end with .VENDOR.XINFO.END.
> 
> Hope springs eternal for this novice perl scripter.  If you can't help me,
> but can refer me to another list which might - I'd appreciate that.
> 
> Pat Gorden-Ozgul
> [EMAIL PROTECTED] / [EMAIL PROTECTED]
> 
> A sample from the datafile:
> 
> .VENDOR_LIBRARY. BNL-MAIN
> .VENDOR_ID. 0000070001
> .VENDOR_NAME. Research Books, Inc. / scholium
> .VENDOR_GROUP2. DOMESTIC
> .VENDOR_ADDR1_BEGIN.
> .PHONE. 800 445 7359   203 245 3279
> .FAX. 203 245 1830
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR1_END.
> .VENDOR_ADDR2_BEGIN.
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR2_END.
> .VENDOR_XINFO_BEGIN.
> .PAYTO. Research Books, Inc. / scholium
> .VENDOR_XINFO_END.
> 
> .VENDOR_LIBRARY. BNL-MAIN
> .VENDOR_ID. 0000070097
> .VENDOR_NAME. Aspen Publishers Inc.
> .VENDOR_GROUP2. DOMESTIC
> .VENDOR_ADDR1_BEGIN.
> .PHONE. (800)234-1660
> .LINE. 7201 McKinney Circle
> .CITY. Frederick
> .STATE. MD
> .ZIP. 21704
> .VENDOR_ADDR1_END.
> .VENDOR_ADDR2_BEGIN.
> .ATTN. Accounts Receivable
> .LINE. P.O. Box 64054
> .CITY. Baltimore
> .STATE. MD
> .ZIP. 21264-4054
> .VENDOR_ADDR2_END.
> .VENDOR_XINFO_BEGIN.
> .PAYTO. Aspen Publishers Inc.
> .VENDOR_XINFO_END.
> 
> .VENDOR_LIBRARY. BNL-MAIN
> .VENDOR_ID. 0000070014
> .VENDOR_NAME. John Wiley and Sons, Inc.
> .VENDOR_GROUP2. DOMESTIC
> .VENDOR_ADDR1_BEGIN.
> .ATTN. Order Department
> .LINE. Eastern Distribution Center
> .LINE2. One Wiley Drive
> .CITY. Somerset
> .STATE. NJ
> .ZIP. 08875
> .COUNTRY. USA
> .PHONE. (800) 225-5945
> .VENDOR_ADDR1_END.
> .VENDOR_ADDR2_BEGIN.
> .ATTN. John Wiley & Sons, Inc.
> .LINE. P.O. Box 18684
> .CITY. Newark
> .STATE. NJ
> .ZIP. 07191-8684
> .COUNTRY. USA
> .PHONE. (908) 469-4400
> .VENDOR_ADDR2_END.
> .VENDOR_XINFO_BEGIN.
> .PAYTO. John Wiley & Sons, Inc.
> .VENDOR_XINFO_END.
> 
> .VENDOR_LIBRARY. BNL-MAIN
> .VENDOR_ID. 0000070001
> .VENDOR_NAME. Research Books, Inc. / scholium
> .VENDOR_GROUP2. DOMESTIC
> .VENDOR_ADDR1_BEGIN.
> .PHONE. 800 445 7359   203 245 3279
> .FAX. 203 245 1830
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR1_END.
> .VENDOR_ADDR2_BEGIN.
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR2_END.
> .VENDOR_XINFO_BEGIN.
> .PAYTO. Research Books, Inc. / scholium
> .VENDOR_XINFO_END.
> 
> .VENDOR_LIBRARY. BNL-MAIN
> .VENDOR_ID. 0000070001
> .VENDOR_NAME. Research Books, Inc. / scholium
> .VENDOR_GROUP2. DOMESTIC
> .VENDOR_ADDR1_BEGIN.
> .PHONE. 800 445 7359   203 245 3279
> .FAX. 203 245 1830
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR1_END.
> .VENDOR_ADDR2_BEGIN.
> .LINE. 38 Academy Street
> .CITY. Madison
> .STATE. Ct
> .ZIP. 06443
> .VENDOR_ADDR2_END.
> .VENDOR_XINFO_BEGIN.
> .PAYTO. Research Books, Inc. / scholium
> .VENDOR_XINFO_END.
> 
> The code:
> 
> ########################################
> # script:  peoplesoft_vendors.pl
> # author:  PGO
> # description:  Generate PeopleSoft 'VN'vendor batch file
> #               from API output
> # date:  01/31/03
> ########################################

You should enable warnings and strict when developing your code.

use warnings;
use strict;


> my $file_in = 'vendshrt';
> 
> my $library = ".VENDOR_LIBRARY. ";
                 ^              ^
> my $library_out = " ";
> my $id = ".VENDOR.ID. ";
            ^         ^
> my $id_out = " ";
> my $name = ".VENDOR_NAME. ";
              ^           ^
> my $name_out = " ";
> my $payto = ".PAYTO. ";
               ^     ^
> my $payto_out = " ";
> my $group2 = ".VENDOR_GROUP2. ";
                ^             ^
> my $group2_out = " ";
> my $phone1 = ".PHONE. " ;
                ^     ^
> my $phone1_out = " ";
> my $fax = ".FAX. ";
             ^   ^
> my $fax_out = " ";
> my $addr1_line = ".LINE. ";
                    ^    ^
You are using these strings as regular expressions however the dot is
special in regular expressions, it matches ANY character (except
newline.)  It would be more efficient to use the qr// operator to
compile the regular expressions before using them.


> my $addr1_line_out = " ";
> 
> my $blank8 = "        ";
> 

> open(DATA, "$file_in") || die ;
             ^        ^
Putting quotes around single variables is almost always wrong.

perldoc -q quoting


>   while (<DATA>)

You need to put the the current line in the $line variable for the
subsequent lines to work.

    while ( my $line = <DATA> )

>   {
>      chomp ($line);
> # debug
> # print "this line is: $_";
> 
>      if ($line =~ /$library/)
>      {
>         $line =~ s/$library//g;

There is no need to do a match AND substitution.  The /g (global match)
option is not required.  You should use \Q (quotemeta) to escape the
literal dots in the string.  You probably want to anchor the match at
the beginning of the string.

       if ( $line =~ s/^\Q$library// )
       {


>         print $line;
>         $library_out = $line;
> # debug
> print "library = $library_out";
>      }
>      if ($line =~ /$payto/)
>      {
>         $line =~ s/$payto//g;
>         $payto_out = $line;
>      }
>      if ($line =~ /$id/)
>      {
>         $line =~ s/$id//g;
>         $id_out = $line;
>      }
>      if ($line =~ /$name/)
>      {
>         $line =~ s/$name//g;
>         $name_out = $line;
>      }
>      if ($line =~ /$group2/)
>      {
>         $line =~ s/$group2//g;
>         $group2_out = $line;
>      }
>      if ($payto_out = " ")
                      ^^^^^
Having warnings enabled would have caught this mistake.  You are
assigning a single space character to the variable $payto_out which is
_always_ true.

       if ( $payto_out eq " " )

>      {
>         $payto_out = substr($name_out,0,39);
> # debug
> print  "Payto = $payto_out";
> 
>      }
>      if ($line =~ /$phone1/)
>      {
>         $phone1_out = $line;
>      }
>      if ($line =~ /$fax/)
>      {
>         $fax_out = $line;
>      }
>      if ($line =~ /$addr1_line/)
>      {
>         $addr1_line_out = $line;
> # debug
> print "addr1_line_out = $addr1_line_out";
> 
>      }
> #  printf "%8s %-40s %-26s \n", $blank8, $payto_out, $addr1_line_out;
             ^^^                  ^^^^^^^
The '%8s' format will fill the field with spaces, you don't have to do
it yourself.

#  printf "%8s %-40s %-26s \n", ' ', $payto_out, $addr1_line_out;


>   my $library = ".VENDOR_LIBRARY. ";
>   my $library_out = " ";
>   my $id = ".VENDOR.ID. ";
>   my $id_out = " ";
>   my $name = ".VENDOR_NAME. ";
>   my $name_out = " ";
>   my $payto = ".PAYTO. ";
>   my $payto_out = " ";
>   my $group2 = ".VENDOR_GROUP2. ";
>   my $group2_out = " ";
>   my $phone1 = ".PHONE. " ;
>   my $phone1_out = " ";
>   my $fax = ".FAX. ";
>   my $fax_out = " ";
>   my $addr1_line = ".LINE. ";
>   my $addr1_line_out = " ";
>   }

You are declaring a bunch on NEW variables here that go out of scope
(are destroyed) once the loop ends (the final bracket.)  They have no
affect on the variables you declared at the beginning of the program.

> close(DATA);


One way to do it is to use "paragraph mode" to read each record:

use warnings;
use strict;

open DATA, $file_in or die "Cannot open $file_in: $!";

$/ = '';  # set paragraph mode on input record separator

while ( <DATA> ) {
    my ($library)    = /^\.VENDOR_LIBRARY\.\s+(.+)/m;
    my ($id)         = /^\.VENDOR_ID\.\s+(.+)/m;
    my ($name)       = /^\.VENDOR_NAME\.\s+(.+)/m;
    my ($payto)      = /^\.PAYTO\.\s+(.+)/m;
    $payto ||= $name;
    my ($group2)     = /^\.VENDOR_GROUP2\.\s+(.+)/m;
    my ($phone1)     = /^\.PHONE\.\s+(.+)/m;
    my ($fax)        = /^\.FAX\.\s+(.+)/m;
    my ($addr1_line) = /^\.LINE\.\s+(.+)/m;

    printf "%8s %-40s %-26s\n", ' ', $payto, $addr1_line;
    }

close DATA;

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to