user3047080
user3047080

Reputation: 3

Having HASH reference issue with perl

Having the following errors:

Name "main::s" used only once: possible typo at line 22.

Not a HASH reference at line 5.

sub addtwo {

    my $params = shift;

    my $s = $params->{structure};

    if ( ref( $s ) eq "LIST" ) {

        $c = 0;

        foreach $e ( @{$s} ) {
            $s->[$c] = addtwo( { structures => $e } );
            $c++;
        }
    }
    elsif ( ref( $s ) eq "HASH" ) {

        if ( scalar keys %{$s} == 0 ) {
            return undef;
        }
        else {
            foreach my $e ( values %{$s} ) {
                $s{$e} = addtwo( { structure => $s->{$e} } );
            }
        }
    }
    else {
        $s = 1;
    }

    return $c;
}

print addtwo(
    [
        { a => 1, b => 2, c => 3 },
        { d => 4, e => 5 },
        [ 6, 7, 8 ],
        9,
        10,
        11,
        [ 12, 13, 14 ]
    ]
);

Upvotes: 0

Views: 295

Answers (1)

Borodin
Borodin

Reputation: 126772

Please add use strict and use warnings 'all' to the top of every Perl program you write. It's not a beginner's safety net: it's essential for any professional programming

As well as %s, you haven't declared $c or $e, and there's very little point in using my at all unless you have at least use strict in effect

To offer a "solution" would be to rewrite most of what you have put down, so I think it's best that you implement the advice that you've been given so far

But please do try, as I wrote in my comment, to write tiny bits of your program at a time, and to make sure that you have a solid basis when you want to add some more functionality

Upvotes: 1

Related Questions