Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

What's wrong with this code? PERL HELP!

by adamadamson (Initiate)
on Jun 29, 2015 at 16:06 UTC ( [id://1132480]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: What's wrong with this code? PERL HELP!
by Corion (Patriarch) on Jun 29, 2015 at 16:17 UTC

    Usually, perl already tells you what it thinks is wrong with some code. What did Perl tell you when you ran the program?

Re: What's wrong with this code? PERL HELP!
by roboticus (Chancellor) on Jun 29, 2015 at 16:27 UTC

    adamadamson:

    It would be hard to tell what's wrong with the code unless we know what it's supposed to do. You really need to provide context! Error messages, sample data, sample output, etc., would go a long way towards making your question something people might try to answer.

    Just for anyone who cares, I reformatted your code below. However, after reformatting your code, I only saw two typos that you could've found yourself had you added use warnings; and use strict; to your program. Other than those, I didn't see anything painfully obvious.

    Without a clue as to what it is supposed to do, though, I'm not going any further. Yeah, I *could* try to analyze the code to figure out what it's supposed to do. But since you already *know* what it's supposed to do, I needn't waste my time on that.

    sub supercede { my $self = shift; my $records = shift; my $supercede = shift; my $ignore = shift; my (%added, $field, $found, $key, $record, @records, @results, $ret +urned, %superceded, $originalid, $value); foreach $returned (@{$records}) { foreach $field (keys %$supercede) { if ($returned->{$supercede->{$field}}) { foreach $key (keys %{$records}) { if (!grep(/^$key/, @{$ignore})) { $superceded{$returned->{$supercede->{$field}}}- +>{$key} = $retruned->{$key}; } else { if (!$superceded{$returned->{$field}}) { $found = 0; foreach $record (@{$records}) { if ($record->{$field} == $returned->{$s +upercede->{$field}}) { $found = 1 last; } } if ($found == 0) { $superceded{$returned->{$supercede->{$f +ield}}}->{$key} = $returned->{$key}; } } } } } else { if (!$superceded{$returned->{$field}}) { $superceded{$returned{$field}} = $returned; } } } } foreach $value (values $superceded) { if (!$added{$value}) { push @results, $value; $added{$value} = 1; } } return @result; }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: What's wrong with this code? PERL HELP!
by Athanasius (Archbishop) on Jun 29, 2015 at 17:19 UTC

    Hello adamadamson, and welcome to the Monastery!

    Building on roboticus’s answer: I believe there are actually three typos: one misspelling, one missing semicolon, and one wrong sigil ($ instead of %).

    I suggest that your next step should be to take the line:

    my (%added, $field, $found, $key, $record, @records, @results, $return +ed, %superceded, $originalid, $value);

    and move each variable declaration to the point in the code where it’s actually needed. (This may be the point where it’s first used, or it may be a point before that — see e.g. Coping with Scoping). When you do this, you will find that only one of these 11 variable declarations needs to be on this line in the code; and one isn’t needed at all (because it’s never used).

    You should also give careful consideration to your variable names. Having a scalar variable named $supercede (which appears to be a hash reference), and a hash variable named %superceded, is a recipe for confusion.

    By the way, when you have a structure like this:

    if (...) { ... } else { if (...) {

    you can in many cases reduce indentation, and so make the code easier to read, by using Perl’s elsif construct (see Compound Statements):

    if (...) { ... } elsif (...) {

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: What's wrong with this code? PERL HELP!
by 1nickt (Canon) on Jun 29, 2015 at 16:12 UTC
Re: What's wrong with this code? PERL HELP!
by AnomalousMonk (Archbishop) on Jun 29, 2015 at 16:13 UTC

    What's wrong with the code? It can't be read! Please Update your original post to use  <c> ... </c> or  <code> ... </code> tags around code, data and input/output. Please see Markup in the Monastery and Writeup Formatting Tips.


    Give a man a fish:  <%-(-(-(-<

Re: What's wrong with this code? PERL HELP!
by stevieb (Canon) on Jun 29, 2015 at 16:13 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found