Re: single line if conditional
by tcf22 (Priest) on Mar 31, 2004 at 15:55 UTC
|
Works like the code says it should. I think you may want to set $hash{name} = "this and that" if $name is empty. I could be wrong. Right now you are doing the reverse. If it has something in it then you do the assignment.
I have a hunch this is what you meant:
$name = param('name');
$hash{name} = 'this and that' if $name eq '';
What do you want this piece of code to do? What happens when you run it?
| [reply] [d/l] [select] |
|
I made another post near the top if you want to see my sample code. I have HTML checkboxes and I want to say something like:: if this checkbox exists (has been checked), save this ($name in this example) into our hash. We are checking our variable against "" to see if it was checked. Maybe this is the problem? Do checkboxes produce something that would NOT eq ""?
| [reply] |
Re: single line if conditional
by Roy Johnson (Monsignor) on Mar 31, 2004 at 15:55 UTC
|
If your version of perl isn't recent, single-line if statements may not work for you.
I would have sworn that statement modifiers were an introduced feature. My mistake.
I notice that you're using a plain string, rather than $name, as the key to the hash. There is nothing syntactically wrong with your snippet, though.
What do you get when you run it?
The PerlMonk tr/// Advocate
| [reply] |
|
Simple statements
...
Any simple statement may optionally be followed by a single
modifier, just before the terminating semicolon. The possible
modifiers are:
if EXPR
unless EXPR
while EXPR
until EXPR
| [reply] [d/l] |
Re: single line if conditional
by Fletch (Bishop) on Mar 31, 2004 at 15:55 UTC
|
DB<1> sub param { return "zorch" }
DB<2> $name = param( "name" )
DB<3> $hash{ "name" } = "this and that" if $name ne "";
DB<4> p $hash{ "name" }
this and that
Works fine. Something Else Is Wrong. perldoc perldebug, make sure that what values you think things have are what you think they are.
| [reply] [d/l] [select] |
Re: single line if conditional
by kutsu (Priest) on Mar 31, 2004 at 16:38 UTC
|
don't know if this will help but I sometimes have proables with the same thing and this is how I solved it:
$hash{"name"} = "this and that" if defined $name; #name has data
#or
$hash{"name"} = "this and that" unless defined $name; #name empty
Update: added empty code and comments
"Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce
| [reply] [d/l] |
Re: single line if conditional
by Tomte (Priest) on Mar 31, 2004 at 15:54 UTC
|
| [reply] |
|
my $maxsize = param('maxsize');
if (param('Submit'))
{
if ($maxsize ne "")
{
$files{"$name"} = "this or that" if $name ne "";
print "somethign should have worked"; # this prints!
}
}
| [reply] [d/l] |
|
| [reply] |
Re: single line if conditional
by runrig (Abbot) on Mar 31, 2004 at 17:11 UTC
|
A checkbox should just be true or false. You should just be able to say: $hash{name} = "this and that" if param('name');
What you have should work though, so I'd check what $name is set to. | [reply] [d/l] [select] |
Re: single line if conditional
by dreadpiratepeter (Priest) on Mar 31, 2004 at 15:55 UTC
|
| [reply] |