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

Hi monks, my subroutine is not doing anything! The values passed into it definately contain data, and i think my syntax is good. Can anyone spot anything?! thanks.
my ($initial_enthalpy, $initial_entropy) = get_initials (\@terminal, \ +@initial_enths, \@initial_entros); sub get_initials { my ($terminal, $initial_enths, $initial_entros) = @_; my ($initial_entropy, $initial_enthalpy); if (($terminal[0] =~ /^G/) || ($terminal[0] =~ /^C/)) { $initial_enthalpy += initial_enths[1]; $initial_entropy += $initial_entros[1]; } return ($initial_enthalpy, $initial_entropy); }

Replies are listed 'Best First'.
•Re: whats wrong with my sub-routine ??
by merlyn (Sage) on Apr 23, 2003 at 15:52 UTC
Re: whats wrong with my sub-routine ??
by Ovid (Cardinal) on Apr 23, 2003 at 16:11 UTC

    By reducing your subroutine to the bare essentials, we appear to get the following (with some variable name munging to make it easier to read):

    my ($initial_enthalpy, $initial_entropy) = get_initials ($terminal[0], $initial_enths[1], $initial_entros[1 +]); sub get_initials { my ($terminal, $enths, $entros) = @_; my ($initial_entropy, $initial_enthalpy); if ( $terminal =~ /^[GC]/) ) { return ($enths, $entros); } }

    Which is the same as:

    my ($initial_enthalpy, $initial_entropy) = $terminal[0] =~ /^[GC]/ ? ($initial_enths[1], $initial_entros[1]) : (undef undef);

    From what I see, your += assignments in the subroutine suggest that you expect these variables to be incremented, even though their initial scope is in this subroutine and thus will be equivalent to zero wheen incrementing. Perhaps if you tell us what you're trying to do (and show us sample inputs and outputs), we can clear this up for you.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: whats wrong with my sub-routine ??
by John M. Dlugosz (Monsignor) on Apr 23, 2003 at 15:55 UTC
    Start by saying
    use strict; use warnings;
    and your typos (using a global variable where you meant to use a parameter) will be pointed out to you.

    —John

Re: whats wrong with my sub-routine ??
by broquaint (Abbot) on Apr 23, 2003 at 15:57 UTC
    Can anyone spot anything?
    You need to dereference your array references e.g
    my @ar = qw/ foo bar baz /; func(\@ar); sub func { my $ar = shift; ## note the dereference via -> print $ar->[0]; } __output__ foo
    See. tye's References quick reference for info references and be sure to use strict to catch these sort of errors.
    HTH

    _________
    broquaint

      Isn't that the same as saying:

      my $ar = qw/ foo bar baz /; func(\@ar); sub func { my $ar = @_; }

      I ask, 'cause that's what he did.

      --Coplan

        Some misunderstandings:

        my $ar = qw/ foo bar baz /;

        In the line above, you're assigning a list in scalar context. You're going to get the last element of that list (baz) assigned to $ar.

        func(\@ar);

        That will throw a warning under strict because you declared a scalar ($ar), not an array (@ar).

        my $ar = @_;

        Now you're accessing an array in scalar context. This will result in $ar being set the the number of elements in @_. Here's a quick demonstration:

        $ perl -e 'sub aa {my $a = @_;print $a}; aa(qw(foo bar baz))' 3

        I realie those seem tricky and frankly, it's often a matter of memorizing how those things behave.

        Cheers,
        Ovid

        New address of my CGI Course.
        Silence is Evil (feel free to copy and distribute widely - note copyright text)