Arvind Baldeo wrote: > > Hi All, Hello,
> 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 > > -------- > #!/usr/bin/perl -w ^^ Enabling warnings is good, "use strict" helps as well. use strict; > $CardID1="$ARGV[0]"; > $CardID2="$ARGV[1]"; ^ ^ Quotes are not appropriate here. my ( $CardID1, $CardID2 ) = @ARGV; > $file="dat"; > $tmpfile="tmp"; my $file = 'dat'; my $tmpfile = 'tmp'; We need a flag to indicate a match was found. my $found = 0; > open(myFILE,"+<$file") || die "Can't open $file:$!\n"; > open(tmpFILE,">>$tmpfile") || die "Can't open $tmpfile:$!\n"; Very good. :-) > 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"; > } Try this instead. if ( /^$CardID1:$CardID2:/ ) { $found = 1; print tmpFILE $_; last; } > } unless ( $found ) { print myFILE " new entry\n"; } > close(tmpFILE); > close(myFILE); The last three lines aren't required. > 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 John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]