Click to See Complete Forum and Search --> : [RESOLVED] reading file help, end of file stop after reading file 10 times, reads files 10 times


winracer
04-01-2009, 02:51 PM
here is the code I am useing. it reads file fine but is reads it about 10 times .
can anyone tell me what I might have missed.

thanks in advance for your help


$location_of_hits_file = "/locationof my file/file.data";


if (-e "$location_of_hits_file") {
open (HITS_FILE, "$location_of_hits_file");

while (<HITS_FILE>)
{
$line = $_;
chop ($line) if ($line =~ /\n$/);
@hitsfields = split (/\|/, $line);
for ($i = 0;$i <= $index_of_last_field;$i++) {

$hitsfields[$i] =~ s/~p~/\|/g;
$hitsfields[$i] =~ s/~nl~/<br>/g;
$num_ad[$i] = $hitsfields[$i];

}


close (NEW_HITS_FILE);
print qq~Date Posted: $num_ad[3] | Ad Number: $num_ad[0] | Cat: $num_ad[21] | Sub : $num_ad[22] <br>

~;



}


}
else{


}

Sixtease
04-02-2009, 12:41 AM
Are you sure you use this code? $index_of_last_field is used but never assigned to (which in this case will give the while loop just one iteration). NEW_HITS_FILE is closed but never opened (which should do no damage). I tried running the code and it doesn't seem to read the file more than once.

How do you run the thing? On console or through web on the server (like CGI)?

winracer
04-02-2009, 07:25 AM
I run it within a perl script. yes maybe this is it $index_of_last_field but not sure what to put there. the files is setup like this data in this expamle will = xxxxx also the xxxxx is only and example it could be for 1 to 1000 letters or numbers in each set of xxxxx


example


xxxxx|xxxxx|xxxxx|xxxxx|xxxxx|etc.....for about 20 times
xxxxx|xxxxx|xxxxx|xxxxx|xxxxx|
etc... for about 200 lines this could change for 200 to 1000s



any thoughts and thanks for your help Sixtease

Sixtease
04-02-2009, 07:53 AM
:-) OK, but how do you execute the Perl script? Do you enter its filename on command line? Do you double click its icon? Do you request its URL in a web browser?

Because if the latter is the case, then I would think that the script itself runs ten times. Then the Perl code would be kinda irrelevant and configuration would be very relevant.

winracer
04-02-2009, 08:22 AM
I do request it in a URL web browser. and this is only part of the script. the whole script only runs one time. thanks again.

Sixtease
04-02-2009, 08:23 AM
OK, then let's look at the whole monster.

winracer
04-02-2009, 08:56 AM
thanks Sixtease your were right!! after I posted my comment that the script only runs one time. I looked and did find that I had placed the code within a another loop.

but I did change code to this also.





#added for expiration date
($dbmonth,$dbday,$dbyear) = split (/\//, $fields[$index_of_modification_time]);
$julian_day = &jday($dbmonth,$dbday,$dbyear);
$expiration_day = $julian_day + $fields[$index_of_ad_duration];
($expmonth,$expday,$expyear,$expweekday) = &jdate($expiration_day);
if ($expmonth < 10) { $expmonth = "0$expmonth"; }
if ($expday < 10) { $expday = "0$expday"; }
if ($european_date_format eq "on") { $expiration_date = "$expday/$expmonth/$expyear"; }
else { $expiration_date = "$expmonth/$expday/$expyear"; }
#stop for expiration date

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
@Days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
@Months = ("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
$day = $Days[$wday];
$date = $mday + 0;
$year = 1900 + $year;
$month = $Months[$mon];
# Format the date any way you want, ie. $date/$month/$year or $year/$date/$month

$new986 = "$month/$date/$year,";


$location_of_hits_file = "/mylocation/file.data";


if (-e "$location_of_hits_file") {
#use strict;
#use warnings;

open (HITS_FILE, "$location_of_hits_file");

while (<HITS_FILE>)
{
$line = $_;
chomp ($line) if ($line =~ /\n$/);
@hitsfields = split (/\|/, $line);
for my $i (0..$#hitsfields) {

$hitsfields[$i] =~ s/~p~/\|/g;
$hitsfields[$i] =~ s/~nl~/<br>/g;
$num_ad[$i] = $hitsfields[$i];

}


close (NEW_HITS_FILE);

print qq~Date Posted: $num_ad[3] | Ad Number: $num_ad[0] | Cat: $num_ad[21] | Sub : $num_ad[22] <br>

~;



}


}
else{


}

if ($num_ad[3] eq "$month/$date/$year")
{
$year1 = "$year";
$month1 = "$month";
$date1 = "$date";

}
else
{

$year1 = "0";
$month1 = "0";
$date1 = "0";
}

print qq~Date Posted: $num_ad[3] $year1,$month1, $date1<br><br>~;





#######################################
#####################################



now on to more. what I am trying to do is to have a new today beside a Category that has subCategories under it with different ads.

might be a long way to go to get what I want but only way I know how do do it.


I got this working so now I have to pull the dates of the ads and compare them to todays date. like this from the code above but I lost the $year1,$month1, $date1 right now...


if ($num_ad[3] eq "$month/$date/$year")
{
$year1 = "$year";
$month1 = "$month";
$date1 = "$date";

}
else
{

$year1 = "0";
$month1 = "0";
$date1 = "0";
}

print qq~Date Posted: $num_ad[3] $year1,$month1, $date1<br><br>~;




again thanks for your help. I know I will be posting again when I get stuck....

Sixtease
04-02-2009, 01:00 PM
Sorry, too much reverse engineering. :-) Could you isolate the current problem? Like show the minimal thing that does not do what you suppose it should and describe what it does and what you want it to do?

winracer
04-02-2009, 02:51 PM
yes, sometimes to easy answer is the one that bites you in the xxx and drive you crazy

again thanks!