On Thursday, October 10, 2002, at 01:12 PM, Diethorn, Robert - MBDC wrote:
> > Greetings all, Howdy. > 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" This is the main reason to use warnings, right here. It's trying to explain the problem to you. At some point in your programs execution, you're trying to use an undefined value in a string. It's happening on line 28 of your script, when the SCHEMAIN handle is on line 62 of that file. Let's go see if we can figure out why... > 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; > > > my $datafile = $ARGV[0]; # read 1st argument into > variable $datafile > my $schemafile = $ARGV[1]; # etc. > my $outputfile = $ARGV[2]; # etc. > > open (DATAIN,"$datafile"); # open file named by $datafile for > reading Be sure and check that an open call succeeds, returns a true value. Typical Perl opens have the form: open DATAIN, "$datafile" or die "Error opening file: $!\n"; > open (SCHEMAIN,"$schemafile"); # open file named by $schemafile for > reading > open (XMLOUT, ">>$outputfile"); # open file named by $outputfile for > writing (appending) Ditto for these two. > 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; > while (<SCHEMAIN>) { # read one line of > schemafile to $_ while lines are available to read I think the above line is your main problem. You're reading in one line of DATAIN, then reading all of SCHEMAIN, an then going back for line two of DATAIN. When you get back here, you're reading from an exhausted filehandle, which returns undef and is likely what's triggering the warning. How about combine this with a couple of lines below and do: chomp(my $newelement = <SCHEMAIN>); > 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 No longer needed. > $newelement =~ s/>a/>$data[$count]/; # search for ">a" in > current line; replace with ">" and a single element from the array > @data > chomp ($newelement); Ditto. Not needed. > print XMLOUT "$newelement\n"; # write new line of > text to outputfile > $count++; # increment variable > $count > } > } > > 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"; Well, that's what jumps out at me on the first scan. See if that helps you along and if it doesn't send it back for round two. Good luck! James Gray > > ____________ > Rob Diethorn > Systems Administrator > McDonough Braungart Design Chemistry, LLC > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]