Increments

He has: 1,380 posts

Joined: Feb 2002

i was wondering how to add a number in increments. example:
i have a script that takes env vars, i would like to number it by visitor...1, 2, 3..etc...i tried $i++, but that doesnt do anything it only adds the "++" at the top...thanks

Busy's picture

He has: 6,151 posts

Joined: May 2001

++ to the top of what?
are you making a hit counter type thing? is the results being written to file or database?
whats the code segment?

He has: 1,380 posts

Joined: Feb 2002

ok..i have a code that takes the env vars and places them in a db (flatfile):

#!/usr/local/bin/perl

use CGI;
my $query = CGI->new();


my $db = "/home/data/user.txt";


open (DB,">>$db");
print DB ("$i++\n");
foreach $key (sort keys(%ENV)) {
print DB ("$key = $ENV{$key}\n");
}
print DB ("\n");
close DB;


my $url = "http://bradyhouse.hypermart.net/home.html";

print $query->redirect("$url");
'

i added the print DB ("$i++\n");', thinking for the first person, it would add the number 1 infront of the env vars....and the second person would be 2 and so on...but it doesnt work, it only prints ++' in front of the env vars

it is sort of a hit counter....just something that i want to see how many people come, and which visitor had what stats...

They have: 601 posts

Joined: Nov 2001

Nah. This won't work.

First off you don't initialize the variable $i to be incremented, and then you don't store it anywhere so Perl doesn't know what it was left at to increment it for the next visitor.

The easiest way to solve this be to create another file just with a number stored in it, and then create a sub to fetch the number, increment it and print it back to the file. This will work as your counter, then.

Hope this helps.

- wil

He has: 1,016 posts

Joined: May 2002

I've just woken up and my brain is in half rotation, but try this code..

#!/usr/local/bin/perl

use CGI;
my $query = CGI->new();


my $db = "/home/data/user.txt";

open(DB, $db);
chomp($current_hits = <DB>);
close(DB);
$current_hits++;
open(DB, ">$db");
print DB "$current_hits\n";
foreach $key (sort keys(%ENV)) {
print DB "$key = $ENV{$key}\n";
}
close(DB);

my $url = "http://bradyhouse.hypermart.net/home.html";

print $query->redirect("$url");
'

He has: 1,380 posts

Joined: Feb 2002

thx

They have: 447 posts

Joined: Oct 1999

print DB ("$i++\n");' is equal to $i . "++\n"
even if it were not a string and did increment $i, you're using the post-increment operator and it would increment AFTER it printed.

try this:
print DB ++$i . "\n";'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.