I have a subroutine that takes a string (an InChI for the chemoinformatics types around), and separates it into several new strings made of parts of sections of the original string.
sub complete_inchi_parse { my $inchi = shift; my @inchibits = split(/\//,$inchi); my @molecules = 1; my @hydrogens; my @pileofinchis; #several more arrays to catch other string fragments foreach my $section (@inchibits) { if ($section =~ /^h/) # if section begins with a marker { @hydrogens = split(/;/,$section); } #other similar if statements that break sections into other arrays } for (my $index=0; $index<scalar(@molecules); $index++) { my $localinchi = $molecules[$index]; if (defined($hydrogens[$index])) { $localinchi = $localinchi."/h".$ +hydrogens[$index];} if (defined($otherarray[$index])) { $localinchi = $localinchi."/q". +$otherarray[$index];} push @pileofinchis,$localinchi; } return(\@pileofinchis); }
This routine works the first time, but in the event of a more simple string being fed into it immediately afterwards, the output string contains sections from the previous iteration. eg. the defined($hydrogens[1]) is true even though this time around there were no hydrogens to split into @hydrogens. Why are the array values persisting beyond the life of the subroutine call?
--Corrections made by author who is a silly twit. Problem remains.
--Scoping error found in one of the other arrays omitted from the above code for brevity. Hopefully the problem is sorted now. Thanks.

In reply to Ghostly subroutine variables haunting me in subsequent calls by moonunit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.