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

Hello!

I've been working on this for a while and I'm absolutely dumbfounded. This function is supposed to search through a hash created from a database pull by searching through the primary keys. However, after an iteration through the loop two entries get added: 0 => {}, 1 => {}. Here's the function:

use DBI; use List:Util; use Math::Complex; my %CLAUSES; sub next_clauseid { my $current_id = @_; foreach my $id ( keys %CLAUSES ) { if ( $CLAUSES{ $id }{ lineno } == $CLAUSES{ $current_id }{ li +neno } and $CLAUSES{ $id }{ clauseno } == ($CLAUSES{ $current_id +}{ clauseno } + 1) ) { return $id; } } return 0; }

Some sample data in %CLAUSES

10950, { behav_behaver => undef, behavioural => undef, character => "MARS", clause => "Did you just swear?", clauseid => 10950, clauseno => 1, exist_existent => undef, existential => undef, lineid => 181, lineno => 181, mat_actor => undef, mat_goal => undef, material => undef, men_phenom => undef, men_senser => undef, mental => undef, rel_attribute => undef, rel_carrier => undef, rel_identified => undef, rel_identifier => undef, relational_attr => undef, relational_ident => undef, verb_sayer => undef, verb_target => undef, verbal => undef, }, 10736, { behav_behaver => undef, behavioural => "", character => "BRIAN", clause => "Yeah, yeah.", clauseid => 10736, clauseno => 1, exist_existent => "", existential => "", lineid => 7, lineno => 7, mat_actor => "", mat_goal => "", material => "", men_phenom => "", men_senser => "", mental => "", rel_attribute => "", rel_carrier => "", rel_identified => "", rel_identifier => "", relational_attr => "", relational_ident => "", verb_sayer => "", verb_target => "", verbal => "", }, 10862, { behav_behaver => undef, behavioural => undef, character => "BRIAN", clause => "I'll call the cops.", clauseid => 10862, clauseno => 1, exist_existent => undef, existential => undef, lineid => 121, lineno => 121, mat_actor => undef, mat_goal => undef, material => undef, men_phenom => undef, men_senser => undef, mental => undef, rel_attribute => undef, rel_carrier => undef, rel_identified => undef, rel_identifier => undef, relational_attr => undef, relational_ident => undef, verb_sayer => undef, verb_target => undef, verbal => undef, },

Thanks for your help!

Replies are listed 'Best First'.
Re: Elements added to hash in for loop
by LanX (Saint) on Dec 13, 2013 at 03:42 UTC
    try including parens
    my ($current_id) = @_;

    otherwise you are using the length of @_ as key and autovivification creates new entries.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      ... you are using the length of @_ as key ...

      nathaniels: This raises the question: Under what circumstances are you calling the  next_clauseid() function with zero parameters?!?

      I hope that you  usewarnings   so that if you employ the common parameter passing conventions advised by LanX and 2teez, you will at least receive a warning message from Perl in case of such a lapse.

        I'm not nathaniels, but ...

        > I hope that you use warnings so that if you employ the common parameter passing ... you will at least receive a warning message from Perl in case of such a lapse.

        which warning?

        DB<102> use warnings;use strict; sub tst { my $scalar=@_; print $sca +lar } DB<103>

        =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        Where can I find these conventions?
      I knew it had to be something simple! Thanks for your help!
Re: Elements added to hash in for loop
by 2teez (Vicar) on Dec 13, 2013 at 05:10 UTC

    Or something like:

    sub next_clauseid { my $current_id = shift(@_); ...
    Note the usage of shift

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Elements added to hash in for loop
by Laurent_R (Canon) on Dec 13, 2013 at 07:51 UTC
    Or yet simpler:
    my $current_id = shift;