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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if data already exists

Status
Not open for further replies.

JackTheRussel

Programmer
Joined
Aug 22, 2006
Messages
110
Location
FI
Hi !
I cant solve this problem by my self and I'll need some help.

I have program which works followin way:
I get information and and I parse that and I get variables:
Code:
$first, $second and $third
and then I insert these variables to the database.

Code:
open(FILE, $filename);
my @lines=<FILE>;

foreach $t (@lines){

my $str = $t;
my ($first, $second, $third) = split(/\s/, $str, 3);

my $sql_string="insert into table set model='$first', stat='$second', layer='$third'";

$sth = $db_connection->prepare($sql_string);
$sth->execute();
}

And I also have table called
Code:
@ecul
where I have samekind of data like I have in $first variables

Now before insert sentence I would like to check do I have already same data in the @ecual table like I have in $first variable.

If there are already same data then do nothing and move to the next record and so on...

But if there arent that data. Then I would like to execute that insert sentence and take them to the database.

How I should do this ? I cant solve this problem?


 
Code:
$match=0;
foreach (@ecual) {
  if ($first eq $_) {
    # got a match
    $match=1;
    last;
  }
}
if ($match != 1) {
 #insert in database
}

This would be better as a hash than an array
Code:
if (!exists $hash{$first}) {
   $hash{$first}=$first;
   &update_database;
}

HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thank you very much!

For now on I'll spend 2 hour a week on CPAN ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top