Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Possible looping or syntax error

by Anonymous Monk
on Dec 23, 2003 at 15:51 UTC ( [id://316636]=perlquestion: print w/replies, xml ) Need Help??

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

Can someone see what's wrong with the following piece of code? In short what this is doing is taking a textfield and searching a database for it's record. If it's there, print it..but if not, fail it. OR..the alternative of 'All' to print all records. I believe the error is somewhere is in ($search eq "Exact"). Does anyone have any ideas what's causing the script to die?
if(param()){ my $name = param('name'); my $update = param('search'); if($search eq "Exact"){ if($name) { if(exists $snailmail{$name} { print "$snailmail{$name}; } else( print "Name not found in directory"); } else( print "Please enter a name to search for"; } if($search eq "All") { foreach (sort keys %snailmail) { print "$_ : $snailmail{$_}"; }

Replies are listed 'Best First'.
Re: Possible looping or syntax error
by Roy Johnson (Monsignor) on Dec 23, 2003 at 15:57 UTC
    This is not valid Perl code. You have parentheses instead of curlies for your elses, and you don't have things balanced. You don't say where $search comes from, and you don't give us any notion of what input yields what output, and how that's different from what you expect.

    The PerlMonk tr/// Advocate
Re: Possible looping or syntax error
by jeffa (Bishop) on Dec 23, 2003 at 16:18 UTC
    Rather than repeat what the others have already said, i'll am going to post a working solution for you play with. Sometimes it helps to have a working example first:
    use strict; use warnings; use CGI qw(:standard); my %snailmail = ( Pam => 'pammy@foo.com', Sal => 'sally@bar.com', Tom => 'tom@there.com', ); print header,start_html('find it'); print start_form, p('Name: ', textfield('name')), p('Fetch: ', popup_menu('search',[qw(Exact All)])), p(submit('fetch')), end_form, ; if (param('fetch')) { if (param('search') eq 'Exact') { my $name = param('name'); my $email = $snailmail{$name}; if ($email) { print p("$name: $email"); } else { print p("'$name' not found in directory"); } } else { print p("$_: $snailmail{$_}") for sort keys %snailmail; } }
    Feel free to ask any questions about this code. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Possible looping or syntax error
by talexb (Chancellor) on Dec 23, 2003 at 16:04 UTC

    First of all, your code doesn't compile. Secondly, your indentation is pretty poor. Here's what I think the code should look like: (Not compiled -- just reformatted using perltidy)

    if (param()) { my $name = param('name'); my $update = param('search'); if ($search eq "Exact") { if ($name) { if (exists $snailmail{$name}) # Missed closing ) { print "$snailmail{$name}"; } else { print " Name not found in directory "); } # Missed open and closing braces } else { print " Please enter a name to search for "; # Nothing here allows a user to enter a name. } # Missed open and closing bracesA } elsif ($search eq " All ") { foreach (sort keys %snailmail) { print "$_ : $snailmail{$_} "; } } # Missed closing brace } # Missed closing brace

    Finally, I'd suggest you do some work running this code through the debugger, trying different values and seeing where it takes you. If you're still stuck after all that, come on back and post again.

    --t. alex
    Life is short: get busy!
Re: Possible looping or syntax error
by bm (Hermit) on Dec 23, 2003 at 15:57 UTC
    Maybe you should try changing
    my $update  = param('search');
    to
    my $search  = param('search');

    hth


    --
    bm
Re: Possible looping or syntax error
by dragonchild (Archbishop) on Dec 23, 2003 at 18:19 UTC
    In addition, you should be using strict and warnings. Also, doing something like the following can help with naming issues.
    if (param()) { my %values; $values{$_} = param($_) for qw( name search ); if ($values{search} eq 'Exact') { if ($values{name}) { if (exists $snailmail{$values{name}}) { } else { } } } elsif ($values{search} eq 'All') { } }

    This makes sure you only enter keynames in once. The more times you type something, the more likely it is you will typo something.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      It's true that the more code, the more potential bugs, but that technique removes perl's ability to warn about variable name typos, unless you're using a locked hash. Better to stick with first-class lexicals in this case.

        Also true. This is why I personally use the following variation:
        my $useful_object = Useful::Class->new; $useful_object->$_($cgi->param($_)) for qw( param1 param2 param3 );

        I get the benefits of hash naming with the benefits of Perl's watchful eye. (Albeit, at runtime and not compiletime, but a decent compromise, imho.)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Possible looping or syntax error
by Roger (Parson) on Dec 23, 2003 at 23:46 UTC
    I suggest you to use a programmer's editor with colour syntax highlighting. When I loaded your code into my editor, I could see immediately that you did not close the double quote on the print "$snailmail{$name}; line. Plus a lot of other problems, such as unbalanced brackets, can be spotted at a glance.

    If you are working on a Windows platform, you could try the excellent freeware editor ConTEXT. And if you are working on Unix platform, you could try the nedit or Emacs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://316636]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found