CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Counter for song totals...

 



JordanTLClive
Deleted

Oct 13, 2000, 10:02 PM

Post #1 of 4 (386 views)
Counter for song totals... Can't Post

Hi
I use a very basic program to maintain a database of the songs I have on my site at http://www.tlclive.com. I am trying to add a counter, so the script displays the total number of songs in the archive. I took the little knowledge I have and tried to apply it and I changed:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
}</pre><HR></BLOCKQUOTE>
to
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
$cnter = $cnter + 1;
}</pre><HR></BLOCKQUOTE>
I had earlier in the program put the following in
$cnter = 0; hoping to start it at zero. Luckily this didnt cause any 500's but it doesnt work. The count never increases. Can someone please help me? Thanks

------------------
-Jordan
http://www.tlclive.com


JordanTLClive
Deleted

Oct 14, 2000, 6:49 PM

Post #2 of 4 (386 views)
Re: Counter for song totals... [In reply to] Can't Post

Ok i figured it out myself. This is what I did. I first opened the data file:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$datafile = "/home/tlclive/tlclive-www/cgi-bin/sounds2/data/tlcsounds.dat";

open(FILE, "$datafile");
{ local $/; $cnter1 = <FILE>; }
close(FILE);
</pre><HR></BLOCKQUOTE>
and saved it to a variable ($cnter1)
Then I used the split to break apart that file (now stored in $cnter1):
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@line = split(/\n/,$cnter1);
foreach $line (@line)
{
$cnter = $cnter + 1;
}
</pre><HR></BLOCKQUOTE>
It works! Magically delicous. If you wanna see the simple thing I was trying to do go to http://www.tlclive.com/newsite/sounds.shtml

------------------
-Jordan
http://www.tlclive.com


Kanji
User

Oct 14, 2000, 8:01 PM

Post #3 of 4 (386 views)
Re: Counter for song totals... [In reply to] Can't Post

Congrats on solving the problem yourself! :)

I always find that to be an enlightening and gratifying thing to do, and hopefully you did too, but I do have some pointers ...

$cnter = $cnter + 1; can be abbreviated to $cnter += 1; or -- even more succinctly -- $cnter++;.

Second, there's no need to read in the entire file for counting purposes. It's a waste of resources for starters and there are better ways including making use of the special variable $. ( dollar-period ) which tells you the current line number ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open FILE, "< $datafile" or die "Cannot open $datafile: $!";
1 while FILE; # Forward to the end of the file
$cnter = $.; # $. is the current line no.
close FILE;</pre><HR></BLOCKQUOTE>


JordanTLClive
Deleted

Oct 14, 2000, 8:35 PM

Post #4 of 4 (386 views)
Re: Counter for song totals... [In reply to] Can't Post

Hey thanks! Im still learning here. I knew there was an abbreviated way but I wasnt sure exactly how. Now Im working on a similar case but a bit more complicated. The same page has a search option but the search is sent to another script (for visual reasons). Either way, I would like a way to count the number of searches, and its jut BOGGLING my mind!. No matter where I put the counter code it is either 0 or non existant. If anyone wants to take a look I can email the script to you but I dont want to post the entire thing to the board Smile Its a little lengthy for a post.

------------------
-Jordan
http://www.tlclive.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives