Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 6th, 2001, 07:32 AM
chinnavi chinnavi is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: India
Posts: 2 chinnavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry


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

Reply With Quote
  #2  
Old February 10th, 2001, 08:19 AM
Adrian2 Adrian2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Location: London, England
Posts: 251 Adrian2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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]

Reply With Quote
  #3  
Old February 11th, 2001, 11:56 PM
chinnavi chinnavi is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: India
Posts: 2 chinnavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
adrian2 :

Thank you very much ...


vijay

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > problem with cookie retreive?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway