Arvind Baldeo wrote: > Hi All, > > Can some one please look at my code ad see what I am doing wrong. > > For some reason it is not working. The idea is to search the file using the > commandline argument. It is supposed to read the file and if a match is > found, it outputs to another file. If a match is not found is appends the > existing file. > > Somehow my script appends to both files. Why? > > commandline %./filename 4040 332 > Thanks > Arvind > > -------- > #!/usr/bin/perl -w >
Always add the use strict pragma to your code. > > $CardID1="$ARGV[0]"; > $CardID2="$ARGV[1]"; You don't have to quote the variables $ARGV[0] and $ARGV[1] > > > $file="dat"; > $tmpfile="tmp"; > open(myFILE,"+<$file") || die "Can't open $file:$!\n"; > open(tmpFILE,">>$tmpfile") || die "Can't open $tmpfile:$!\n"; > while (<myFILE>){ #each line is assigned to $_, the default value > > if ((/$CardID1/) && (/$CardID2/)){ > print tmpFILE $_; > $dbase=" existing record\n"; > }else {#entry does not exist, then add > $dbase=" new entry\n"; > } > } > close(tmpFILE); > close(myFILE); > > open(myFILE,">>$file") || die "Can't open $file:$!\n"; > print myFILE "$dbase"; > close(myFILE); > --------- > dat file has the following entry- > > 4040:988:2:1:jane > 4040:332:2:2:ken > 4040:567:2:3:junior What are the contents of "dat" and "tmp" after the script is run. After I ran your script this is what I ended up with (used the same command line you have specified and the same "dat" file) "dat" 4040:988:2:1:jane 4040:332:2:2:ken 4040:567:2:3:junior new entry "tmp" 4040:332:2:2:ken > > > -- > 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]