Robert - Mbdc Diethorn wrote:
> 
>         Greetings all,

Hello,

>         This is my first production Perl script, and I'm having some trouble
> with nested while loops. I'm hoping that someone here can point me in the
> right direction.
>         I'm using the script to merge two files and create a third. The
> first is a delimited text export from a DB, the second is a list of XML
> fields, and the intended output is an XML file. Simple, right? Well, at the
> moment I have two problems. If I use "warnings", the script produces the
> following error: "Use of uninitialized value in concatenation (.) or string
> at line 28, (SCHEMAIN) line 62"
> 
> It does produce output, but not as I expected, which is the second issue.
> The script processes one line of the input correctly, but then stops,
> apparently dying after one iteration of the second while loop.
>         I imagine this problem involves the declaration/redec of $_, but I
> just can't figure out why this can't work. Anyone have any ideas as to
> what's wrong and how I might fix it?
> 
>         Here are my system vitals:
>         Win32
>         ActiveState ActivePerl 5.6.1.631
> 
> I appreciate any help the wizards can offer.
> 
>         Rob
> 
> #!/usr/bin/perl
> use strict;
> use warnings;

use Fcntl ':seek'; # for seek() macros


> my $datafile = $ARGV[0];                        # read 1st argument into
> variable $datafile
> my $schemafile = $ARGV[1];                      # etc.
> my $outputfile = $ARGV[2];                      # etc.

die "usage: $0 datafile schemafile outputfile\n" unless @ARGV == 3;
my ( $datafile, $schemafile, $outputfile ) = @ARGV;


> open (DATAIN,"$datafile");              # open file named by $datafile for
> reading
> open (SCHEMAIN,"$schemafile");          # open file named by $schemafile for
> reading
> open (XMLOUT, ">>$outputfile");         # open file named by $outputfile for
> writing (appending)

You should _always_ verify that the files were opened.

open DATAIN,   '<', $datafile   or die "Cannot open $datafile: $!";
open SCHEMAIN, '<', $schemafile or die "Cannot open $schemafile: $!";
open XMLOUT,  '>>', $outputfile or die "Cannot open $outputfile: $!";


> print XMLOUT "<substance>\n";           # print opening substance tag to
> outputfile
> 
> while (<DATAIN>) {
>         chomp;
>         my @data = split (/~/,$_);              # read line from file called
> by DATAIN into array @data; assign elements split by "~" character
> 
>         my $count = 0;

You need to reset the SCHEMAIN file handle each time through the outer
loop.

         seek SCHEMAIN, 0, SEEK_SET;


>         while (<SCHEMAIN>) {                            # read one line of
> schemafile to $_ while lines are available to read
>                 my $length = @data;                     # assign the number
> of array elements to variable $length
>                 my $newelement = $_;                    # assign the current
> line of input from schemafile to variable $newelement
>                 $newelement =~ s/>a/>$data[$count]/;    # search for ">a" in
> current line; replace with ">" and a single element from the array @data
>                 chomp ($newelement);
>                 print XMLOUT "$newelement\n";           # write new line of
> text to outputfile
>                 $count++;                               # increment variable
> $count

This can be written in two lines:

                s/>a/>$data[$count++]/;
                print XMLOUT;


>         }
> }
> 
> print XMLOUT "</substance>\n";          # print closing substance tag and
> newline to outputfile
> 
> close (XMLOUT);
> close (SCHEMAIN);
> close (DATAIN);
> 
> print "datafile = $datafile and schemafile = $schemafile and outputfile =
> $outputfile";



John
-- 
use Perl;
program
fulfillment

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

Reply via email to