Forum Moderators: coopster & phranque

Message Too Old, No Replies

Only count lines starting with .....

Only count lines starting with .....

         

StopSpam

4:20 pm on Jul 14, 2003 (gmt 0)

10+ Year Member



Only count lines starting with .....

Ok i got this code wish counts the amount of lines in a file

open ( FH, "news.txt" ) ¦¦ die "Couldn't open that file\n";
while ( <FH> ) {
$count++;

}

$count # contains now amount of lines in file: news.txt

But now i want to count lines that only start with the worth "today"

so i must try something like:

open ( FH, "news.txt" ) ¦¦ die "Couldn't open that file\n";
while ( <FH> ) {
for (@File) {
if ($_ =~ /^$search/);
$count++;
push(@contents,$_);
}
}

but this aint work it gives error or counts all lines with "today" works the other way around ... hehehe

but iwant to count only the lines starting with "today"
please advise ;-)
thx

gettoefl

4:52 am on Jul 15, 2003 (gmt 0)

10+ Year Member




Try

if ($_ =~ /^${search}/);

It is confused and thinking $ is end of line

Mark

StopSpam

10:28 am on Jul 15, 2003 (gmt 0)

10+ Year Member



Neh that aint work to ...
i get a syntax error then

12 if ($_ =~ /^${search}/);
Line 12 syntax error at /home/web/www/bfa/clean.cgi line 12, near ");"

Here is the full code why?
aint it counting the lines starting with $search
what do i do wrong here?
#######
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";

$search = "Today";
$file = "news.dat";

open (F, "$file") ¦¦ die "Can't open - $!\n";
flock(F,2);
for (@File) {
# if ($_ =~ /^$search/) {
push(@contents,$_);
$count++;
}
}

print "the file ($file) contains ($count) lines stating with worth: ($search) \n";

ShawnR

10:33 am on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



12 if ($_ =~ /^${search}/);
or
12 if ($_ =~ /^${search}/){

ShawnR

11:33 am on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, you probably need to set @File

e.g. @File = <F>;

StopSpam

7:23 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



i now made this code for count only lines starting with ....

#!/usr/local/bin/perl
#####################
print "Content-type: text/html\n\n";
$file = "news.dat";
$when = "TodaY";

open ( FH, "$file" ) ¦¦ die "Couldn't open that file\n";
while ( <FH> ) {
if ($_ =~ /^$when/) {
$count++;
}

print "i found:$count lines starting with: $when <BR>\n";

}

It counts lines starting with $when but it looks like its loping and still count the other lines as well.

i have a datafile news.dat inside you find this data:

TodaY
TodaY
TodaY
TodaY
TodaY
TodaY
TodaY
TodaY
TodaY
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow
Tomorow

now when i rutn the above script its not priting
i found 9 lines ... it keeps looping it print this:

i found:1 lines starting with: TodaY
i found:2 lines starting with: TodaY
i found:3 lines starting with: TodaY
i found:4 lines starting with: TodaY
i found:5 lines starting with: TodaY
i found:6 lines starting with: TodaY
i found:7 lines starting with: TodaY
i found:8 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY
i found:9 lines starting with: TodaY

===

While it should print only this:

i found:9 lines starting with: TodaY

whats wrong why looping true each line etc....?

ShawnR

10:37 pm on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The print statement should be outside the while loop. i.e.

open ( FH, "$file" ) ¦¦ die "Couldn't open that file\n";
while ( <FH> ) {
if ($_ =~ /^$when/) {
$count++;
}
}

print "i found:$count lines starting with: $when <BR>\n";

StopSpam

10:20 am on Jul 16, 2003 (gmt 0)

10+ Year Member



yep your right works now ..

i where thinking loop ends behind first }
but its the second }

thank you ;-)

the code has 2x { so there must be 2x } as well to close it ....

these are the little things beginers must pay extra atention to ...
these are easy to make mistaks you do not have to make ...

ShawnR

10:39 am on Jul 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way to keep the braces under control is to format your code so it is obvious. Good text editors will help do this. e.g.:

open ( FH, "$file" ) ¦¦ die "Couldn't open that file\n";
while ( <FH> )
{
____if ($_ =~ /^$when/)
____{
________$count++;
____}
}

(substitute the ____ with spaces)