On Fri, 15 Dec 2000, Mike Prozinski wrote: > Hello. > > Here's what I am trying to do: > Read through a file that has lines generally of the form: > > "key" "value"; > > If I come across a key that I have stored in a hash (which I build from > another file), I want to replace the "value" in the file with the one from > the hash. I am having trouble doing this replace. Everything else seems to > be working, (i.e. building the hash of keys/values, finding the lines in the > file that have a value I want to replace, etc.) The file never seems to get > changed. Any help would be appreciated. Also, any advice/pointers on how you > might do anything in the script differently is also. welcome > > Attached are the two files I am processing, and the script. The script is > also listed below. Many thanks! > > > #---- Start script ---- > > use strict; > use FileHandle; > > use vars qw( $dataDir $scriptDir $last_run $datalogDir $wfFileBaseName > $recordNum $WF $DL $dlDefinitionFile $dlFile > $key $value > > %avgPoints %seenIt > ); > > > $wfFileBaseName = "avgPoints"; #base name of file created from wf. > $dlDefinitionFile = "WOTdatalog.dlog"; > > #Get datalog file name. For now, it is the datalog name with .dl on it > $dlFile= $dlDefinitionFile . ".dl"; > > #Open file for read/write access > $DL = (new FileHandle "+< $dlFile") or die "can't open datalog file > $dlFile"; > > print "Datalog file name is $dlFile\n"; > > #Read through the schema portion that I am not interested in. > while (<$DL>){ > chomp; > last if /^}/; #Process schema; > } > > #Now read the records > while (<$DL>){ > > chomp; > > next if /^{/; > > if (/^\"(.*\d)\"$/){ > print "Record num is: $1\n"; > $recordNum = $1; > > wfRead($recordNum); #Read the wf file corresponding to this record >number > > while (<$DL>){ > > next if /^{/; #Skip opening record { > last if /^}/; #Done with record if encounter } > > if (/^\t"(.*?)"\s+?"(.*?)"/){ #Extract pointname, values > $key = $1; > $value = $2; > > # print "Got $key in the hash\n" if $seenIt{$key}; #This >also works > if (defined $avgPoints{$key}){ > > print "\t\"$key\"\t\"$avgPoints{$key}\"\;\n"; >#This is what I want the > line in the file to be replaced with > > # ~~~~ PROBLEM HERE... file doesn't seem to get changed?? > $DL->print >("\t\"$key\"\t\"$avgPoints{$key}\"\;\n"); > # ~~~~ > } > } > } > } > } > > > #Read the key/value pairs from the other file. > sub wfRead{ > my $recordNum = shift; > my $WF = (new FileHandle "< $wfFileBaseName.$recordNum") or die "Can't > open wf file $wfFileBaseName.$recordNum\n"; > > my $key; > my $value; > > my %points; > my %gotIt; > > while (<$WF>){ > chomp; > ($key, $value) = (/^(.*)\="(.*?)"/); #Do it with regexp! > $points{$key} = $value; > $gotIt{$key} = 1; > } > > $WF->close; > > %avgPoints = %points; > %seenIt = %gotIt; > > } > __END__ > Rather than opening the file for both input and output, open two files, one for reading and another with the same file name for output. Copy all the lines from the input file to the output file. As you are reading records from the file that you are not changing print them to the output file. When you are reading the part of the file that you may change, when you find a record you want to change, change it before printing it to the output file. After you are done reading the input file, close the output file. One possible addition is to set a variable if you change any record. Then after you are done reading the input file, only close the output file if you changed one or more records. This addition can be skipped if you always change at least one record or you don't care if the output file is a copy of the input file. if you close the output file, it will replace the input file. **** [EMAIL PROTECTED] <Carl Jolley> **** All opinions are my own and not necessarily those of my employer **** _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users