S
Stefan Glimne
Hello 2 u all,
I have a really interesting problem that I get when I program my
html-pages with perl.
The problem is that the perl-program doesn't follow the if-statement.
for example.
1. I get info from a standard text-file database
2. I store an hash like , $imdblink{$imdbid} = $value , from the
database
3. Then I have a new number $imdbid_new that I want to check if it's in
the $imdblink-hash like ,
if ($imdblink{$imdbid_new}) {
"if it exist then this should happen"
} else {
"if it doesn't exist then this should happen"
}
The problem is that it doesn't exist when the test is done, and that is
a fact. I then store the data for the $number_new within the
else-statement in the same database. And I don't even set the
$example{$imdbid_new}.
The program now says that the $imdblink{$imdbid_new} exist, even if I
don't set it under "point 2"
As I see it the program checks if it doesn't exist and then continues to
the else-statement, but then suddenly get the idea that it did exist,
because I saved it in the database-file, and goes back to the if-stetement.
Is there an explanation to this?
Here is the example that does not work:
##############################################
#
# Load Films in FilmDataBase
#
$file = "db\\filmdatabase_comp.txt";
open (INDATA, "$home_dir$file") or die "Can't open $home_dir$file: $!\n";
while ($line = <INDATA>) {
chop $line;
($key,$value) = split(" = ", $line);
$key2 = $filmid . '_' . $key;
$filmdatabase{$key2} = $value;
if ($key eq "imdblink") {
($key,$imdbid) = split('\?', $line);
$imdblink{$imdbid} = $filmid;
}
}
close INDATA;
if ($in{'update'} eq "IMDB") {
($key,$imdbid) = split('\?', $in{'imdblink'});
if ($imdblink{$imdbid}) {
print "File already exist with the name
$filmlista{$imdblink{$imdbid}}<BR>\n";
} else {
... reading and storing data in hashes (but not %imdblink .....
$file = $home_dir;
$file .= "db\\filmdatabase_comp.txt";
$file_tmp = $home_dir;
$file_tmp .= "db\\filmdatabase_comp.tmp";
rename($file, $file_tmp);
open (UTDATA_TMP, "$file_tmp") or die "Can't open
$home_dir$file_tmp: $!\n";
open (UTDATA, ">>$file") or die "Can't open $home_dir$file: $!\n";
while ($line = <UTDATA_TMP>) {
chop $line;
if ($line eq "##########") {
print UTDATA "##########\n";
$line = <UTDATA_TMP>;
chop $line;
($key,$filmid) = split(" = ", $line);
if ((int($filmid) + 1) == int($in{'filmnumber'})) {
print UTDATA "$line\n";
for ($i = 1; $i <= 9; $i++) {
$line = <UTDATA_TMP>;
print UTDATA "$line";
}
print UTDATA "##########\n";
print UTDATA "filmnumber = $filmdatabase{$in{'filmnumber'} .
'_filmnumber'}\n";
print UTDATA "filmname = $filmdatabase{$in{'filmnumber'} .
'_filmname'}\n";
print UTDATA "year = $filmdatabase{$in{'filmnumber'} .
'_year'}\n";
print UTDATA "imdblink = $filmdatabase{$in{'filmnumber'} .
'_imdblink'}\n";
print UTDATA "imdbrating = $filmdatabase{$in{'filmnumber'} .
'_imdbrating'}\n";
print UTDATA "runtime = $filmdatabase{$in{'filmnumber'} .
'_runtime'}\n";
print UTDATA "genre = $filmdatabase{$in{'filmnumber'} .
'_genre'}\n";
print UTDATA "actors = $filmdatabase{$in{'filmnumber'} .
'_actors'}\n";
print UTDATA "directors = $filmdatabase{$in{'filmnumber'} .
'_directors'}\n";
print UTDATA "writers = $filmdatabase{$in{'filmnumber'} .
'_writers'}\n";
} else {
print UTDATA "$line\n";
}
} else {
print UTDATA "$line\n";
}
}
close UTDATA;
close UTDATA_TMP;
# unlink $file_tmp;
}
$in{'update'} = "YES";
}
##############################################
I have a really interesting problem that I get when I program my
html-pages with perl.
The problem is that the perl-program doesn't follow the if-statement.
for example.
1. I get info from a standard text-file database
2. I store an hash like , $imdblink{$imdbid} = $value , from the
database
3. Then I have a new number $imdbid_new that I want to check if it's in
the $imdblink-hash like ,
if ($imdblink{$imdbid_new}) {
"if it exist then this should happen"
} else {
"if it doesn't exist then this should happen"
}
The problem is that it doesn't exist when the test is done, and that is
a fact. I then store the data for the $number_new within the
else-statement in the same database. And I don't even set the
$example{$imdbid_new}.
The program now says that the $imdblink{$imdbid_new} exist, even if I
don't set it under "point 2"
As I see it the program checks if it doesn't exist and then continues to
the else-statement, but then suddenly get the idea that it did exist,
because I saved it in the database-file, and goes back to the if-stetement.
Is there an explanation to this?
Here is the example that does not work:
##############################################
#
# Load Films in FilmDataBase
#
$file = "db\\filmdatabase_comp.txt";
open (INDATA, "$home_dir$file") or die "Can't open $home_dir$file: $!\n";
while ($line = <INDATA>) {
chop $line;
($key,$value) = split(" = ", $line);
$key2 = $filmid . '_' . $key;
$filmdatabase{$key2} = $value;
if ($key eq "imdblink") {
($key,$imdbid) = split('\?', $line);
$imdblink{$imdbid} = $filmid;
}
}
close INDATA;
if ($in{'update'} eq "IMDB") {
($key,$imdbid) = split('\?', $in{'imdblink'});
if ($imdblink{$imdbid}) {
print "File already exist with the name
$filmlista{$imdblink{$imdbid}}<BR>\n";
} else {
... reading and storing data in hashes (but not %imdblink .....
$file = $home_dir;
$file .= "db\\filmdatabase_comp.txt";
$file_tmp = $home_dir;
$file_tmp .= "db\\filmdatabase_comp.tmp";
rename($file, $file_tmp);
open (UTDATA_TMP, "$file_tmp") or die "Can't open
$home_dir$file_tmp: $!\n";
open (UTDATA, ">>$file") or die "Can't open $home_dir$file: $!\n";
while ($line = <UTDATA_TMP>) {
chop $line;
if ($line eq "##########") {
print UTDATA "##########\n";
$line = <UTDATA_TMP>;
chop $line;
($key,$filmid) = split(" = ", $line);
if ((int($filmid) + 1) == int($in{'filmnumber'})) {
print UTDATA "$line\n";
for ($i = 1; $i <= 9; $i++) {
$line = <UTDATA_TMP>;
print UTDATA "$line";
}
print UTDATA "##########\n";
print UTDATA "filmnumber = $filmdatabase{$in{'filmnumber'} .
'_filmnumber'}\n";
print UTDATA "filmname = $filmdatabase{$in{'filmnumber'} .
'_filmname'}\n";
print UTDATA "year = $filmdatabase{$in{'filmnumber'} .
'_year'}\n";
print UTDATA "imdblink = $filmdatabase{$in{'filmnumber'} .
'_imdblink'}\n";
print UTDATA "imdbrating = $filmdatabase{$in{'filmnumber'} .
'_imdbrating'}\n";
print UTDATA "runtime = $filmdatabase{$in{'filmnumber'} .
'_runtime'}\n";
print UTDATA "genre = $filmdatabase{$in{'filmnumber'} .
'_genre'}\n";
print UTDATA "actors = $filmdatabase{$in{'filmnumber'} .
'_actors'}\n";
print UTDATA "directors = $filmdatabase{$in{'filmnumber'} .
'_directors'}\n";
print UTDATA "writers = $filmdatabase{$in{'filmnumber'} .
'_writers'}\n";
} else {
print UTDATA "$line\n";
}
} else {
print UTDATA "$line\n";
}
}
close UTDATA;
close UTDATA_TMP;
# unlink $file_tmp;
}
$in{'update'} = "YES";
}
##############################################