in reply to Multiple CGI Objects w/ Same Name

I had a similar problem recently

I wrote somthing like:-

my $gender = 'M';
print $gender;
$gender = 'F';
print $gender;

But then I was asked to compare the two values, and set
them equal if they were different

my $gender1 = 'F';
my $gender2 = 'M';

print $gender1;
print $gender2;

if $gender1 eq $gender2
{print "same gender"}
else
{ $gender1 = $gender2 }

So is it ok to have a variable take two diffrent values or should i create a separate variable for each value, as in the second example?

Replies are listed 'Best First'.
Re: Re: Multiple CGI Objects w/ Same Name
by synapse0 (Pilgrim) on Jul 11, 2001 at 22:27 UTC
    It depends on what you need to do with the values. Basic assignment to a scalar ( $var ) will lose anything that was in there before, so if you had $var = "first" and then did $var = "second" you just lost the "first" info. In your example, there's no way to conmpare the two. So if you need to keep the info, use another variable for other info.. If you don't care what was in the variable before, then overwriting it is fine... -Syn0