|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
hai i wrote this for getting my cookie value. seems to be some problem ... where it is? im passing value .... suppose a cookie named "as" is there... i have to get it thro' $cookie_value = &cookie('as'); whereis the problwm? ------------------------------------------ sub cookie { my ($name) = @_; my $cookie_value = $ENV{'HTTP_COOKIE'}; @array = split /;/ , $cookie_value ; for($i=0;$i<$#array;$i++) { my($key,$value) = split /\=/ , $array[$i]; $hash{$key} = $value; } return $hash{$name}; } -------------------------------------------- vijay |
|
#2
|
|||
|
|||
|
Obi-wan!
The problem is your for loop that goes through the array values. $#array is the index of the last element of the array, not the number of elements in the array. So a correct for loop that produced all the indexes would be:
for ($i=0; $i <= $#array; $i++) { } ie. less than or equal, not just less than. But a far better way to work through an array is to use foreach. eg. foreach $value (@array) { } So you don't really need to get the indexes at all as you were only using them to reference the array values. Here's a working code example: Code:
$first = &cookie('first');
$second = &cookie('second');
print "First: $first\nSecond: $second\n";
exit;
sub cookie
{
my ($name) = @_;
my $cookie_value = "first=this;second=that;"; # just for testing!
@array = split(/;/, $cookie_value);
foreach $cookie (@array) {
my ($key, $value) = split (/=/, $cookie);
$hash{$key} = $value;
}
return ($hash{$name});
}
Also, you don't need to escape the = sign in a regular expression as it's not a metacharacter, but escaping it won't cause it to fail because a single backslash on its own is ignored. By the way, "Obi-wan" is a slang term for a programming error that causes something to be "out by one", a very common mistake. In this example, the program reads every value of the cookie except the last one, causing it to work mostly, but not always. [Edited by Adrian2 on 02-10-2001 at 07:22 AM] |
|
#3
|
|||
|
|||
|
adrian2 :
Thank you very much ... vijay |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > problem with cookie retreive? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|