http://qs321.pair.com?node_id=341320

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am used to writing multi-line IF statesments which bogs the code so I'm trying to convert some of them into one line. But for some reason, this attempt which I thought I was sure would work, doesn't. Is there a syntax problem?
$name = param('name'); $hash{"name"} = "this and that" if $name ne "";

Replies are listed 'Best First'.
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?

    - Tom

      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 ""?
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
      If your version of perl isn't recent, single-line if statements may not work for you.

      Well, if by recent you mean a version of Perl >= 1.0. Quoting from perl.man.1 from the 1.0 source:

      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
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.

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

Re: single line if conditional
by Tomte (Priest) on Mar 31, 2004 at 15:54 UTC

    Works for me; how does it fail?

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      it fails not storing anything in the database. I am using strict and warnings but no-go.

      I have a form with a field 'maxsize' and a button 'Submit'.

      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! } }
        assuming that the lastest code is a copying error and $name should read $maxsize, then the problem is not in the if statement, it is later in the code when you use %files to do something.
        modifying your print to include $files{$maxsize} would verify this.
        If the $name is in your existing code, then that explains your problem. Because $maxsize is the variable with the data.


        -pete
        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
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.
Re: single line if conditional
by dreadpiratepeter (Priest) on Mar 31, 2004 at 15:55 UTC
    You don't say what value you are reading in, or why you feel that you got the wrong result. Also, have you tried printing the value of $name before the if to verify that you are getting the param right? (which is the more likely problem).
    As always, are you using strict?


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."