Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Looping structure

by Anonymous Monk
on Nov 28, 2003 at 02:29 UTC ( [id://310630]=perlquestion: print w/replies, xml ) Need Help??

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

Can someone tell me where I'm missing or where I have too many brackets? It just keeps telling me there is an error somewhere but I can't figure out which line(s) it's on.
if ( exists $list{$location} ) { my $value = $list{$location}; $value++; $list{$location} = $value; } elsif ( $list{$location} eq "" ) { if ( exists $list{Unknown} ) { my $value = $list{Unknown}; $value++; $list{Unknown} = $value; } } else { $list{Unknown} = "1"; } else { $list{$location} = "1"; }

Replies are listed 'Best First'.
Re: Looping structure
by pg (Canon) on Nov 28, 2003 at 02:33 UTC

    The problem is line 17, that "else" at line 17 has nothing to match, all the brackets before that line matched perfectly.

    Had you well indented your code, you probably figured out this yourself:

    if ( exists $list{$location} ) { my $value = $list{$location}; $value++; $list{$location} = $value; } elsif ( $list{$location} eq "" ) { if ( exists $list{Unknown} ) { my $value = $list{Unknown}; $value++; $list{Unknown} = $value; } } else { $list{Unknown} = "1"; } =document else { $list{$location} = "1"; }

    How to fix? it really depends on what you want, and I don't want to second guess.

    By looking at the exsiting code, the logic looks broken. I don't think it will ever run into that elsif branch. If $list{$location} eq "", obviously exists($list{$location}).

Re: Looping structure
by Roger (Parson) on Nov 28, 2003 at 04:29 UTC
    Other monks have pointed our your source of error. I just want to say that you are doing a lot of unnecessary work. For example, the code
    my $value = $list{$location}; $value++; $list{$location} = $value;
    can be written as -
    $list{$location}++;
    And your entire code block can be simplified to just one line of Perl (assuming your $location is never '0') -
    $list{ $location || 'Unknown' }++;
Re: Looping structure
by The Mad Hatter (Priest) on Nov 28, 2003 at 02:36 UTC

    I don't understand how you can't figure out what line the error is on. Read the error, it tells you:

    tom@wintermute tom $ perl -c t syntax error at t line 17, near "else" t had compilation errors.
    Reformatted code:
Re: Looping structure
by sunadmn (Curate) on Nov 28, 2003 at 04:10 UTC
    It looks like my fellow monks have corrected you, but I would like to offer a helpfull perl hint. When wanting to test a script before running it try this at the command line
    perl -cwTM-strict ./yourscript.pl
    This will help debug your code without actually running it and possiblyt messing something up.

    USE PERL
Re: Looping structure
by chanio (Priest) on Nov 28, 2003 at 17:52 UTC
    Most programming difficulties might come from having an incomplete knowledge base or not having the right tools to work with.

    You might need XEmacs that points you all the ({[ pairs for easy visualizing them. There are also other editors that do the same. But XEmacs, once you get used to it looks very much more an expert in perl than oneself :) .

    By using -w at the starting line:

    #!/use/bin/perl -w ##and followed by use strict;

    Might help a lot. But remember that you might need to add some more conditionals to your sentences:

    if (($location) && exists($list{$location})) { my $value = $list{$location}; $value++; $list{$location} = $value if ($value);

    ...for example, just to illustrate the use of verified existing variables.

    But then, the error messages are very descriptive and easy to track down.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found