On Thursday, October 10, 2002, at 02:28 PM, Diethorn, Robert - MBDC wrote:
> > Ding! Progress is good. Don't worry, we'll get it there, eventually. > > Many thanks James. I was so fixated on the second loop I failed to > see that the undef really occurred in the return to the first. > Unfortunately, I think I need to retain the second loop. I need to > process the file $schemafile one line at a time, as every line of that > file > contains exactly one XML tag. Most of the "work" in the script is > actually > accomplished by the statement $newelement =~ s/>a/>$data[$count]/; -- > within that second loop. Okay, help me understand a little better then. Are we going to process every line in SCHEMAIN for every line in DATAIN? This would make the output contain x copies of the entire SCHEMAIN file, where x is the number of lines in DATAIN. That what your aiming for? > Is there any was I can set a marker for $_ in the original while > loop to avoid the undef and return to process the next line? Or am I > stuck > with slurping the first file into an array in its entirety and > processing > only the second file with a loop? You can, but I don't think this is needed. Let me see what your above answer is first, before we go here. James > > Thanks for your help, > > Rob > > > > -----Original Message----- > From: James Edward Gray II [mailto:[EMAIL PROTECTED]] > Sent: Thursday, October 10, 2002 2:33 PM > To: Diethorn, Robert - MBDC > Cc: '[EMAIL PROTECTED]' > Subject: Re: Newbie - reading from files in nested while loops > > > On Thursday, October 10, 2002, at 01:12 PM, Diethorn, Robert - MBDC > wrote: > > [snip] > > >> >> #!/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] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]