Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if info exists before adding to file?

Status
Not open for further replies.
Apr 13, 2006
9
GB
Hey,

I use this enter the info I need into my flatfile

&send_error("UT OH I SCREWED UP!.") unless (open GFILE, ">>$config{'basepath'}/games/gamers.txt");
print GFILE "$gamer\n";
close GFILE;


$gamer contains a players name that came from a form just incase you needed that info. How can I check if the gamers name is already in the database and if it is then delete it from the database then add the name at the bottom of the list like it was going too, please??


 
what have you tried so far? and lets see what the file looks like.
 
Ive got it its ok, thank you so much for your help though.

I found a helpful piece of code online which ive altered to delete the name if it already exists:

# Read in and store file contents...
open(FH,'<gamers.txt');
my @data = <FH>;
close(FH);

# User to delete...
my $gamername_to_be_deleted = $gamer;

# Now open for for write (this is replace the file).
open(FH,'>gamers.txt');
foreach my $line (@data)
{


# Only write users to file that don't match username_to_be_deleted...
($gamer)=split(/\|/,$line);
unless ($gamer eq $gamername_to_be_deleted)
{
print FH $line;
}
}

close(FH);
 
Well done Kelly

Just beware that @data = <FH> is not "scalable", that is, as the file gets larger, so does the required memory. At some point you might run out.

So, unless you can guarantee a small file, might be better to process each line at time.

~ Michael
 
Kelly

To make your flat file behave more like a database, you can read it into a hash, change it, and write it out again. But wardy66 is right, if you have a couple of hundred users it's OK, if you have 35,000 then you'll need a real database.

Here's a sample - it just prints the 'output file' to the console, but you can easily change that.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $newgamer = "kelly";
my %db;                  # hash to hold records

while (<DATA>) {
    /^(\w+)/;            # get the name to use as a key
    $db{$1} = $_;        # save the whole record under that key
}

# adds the new gamer if they don't exist, updates them if they do

$db{$newgamer} = "$newgamer 0 0 0\n";

# print out the (sorted) file

print $db{$_} foreach (sort keys %db);

__DATA__
kelly 5 0 3
fred 7 5 3
steve 5 6 7

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
not even 35,000 should be a problem, a million maybe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top