Somewhere in the following script, a variable is not being undefined causing the loop to display too much. I have been trying various ways to undefine the variables that I think are causing the problem without success. I thought the module was working fine, since I tested it before putting it into use, but not in a loop. I am very stumped. Any help would be appreciated.

I have spent over an hour trying to fix this. I am sorry it is so long. If you see any other issues, please let me know. Thank you in advance and have a cookie while you are reading.

Classes.pm

The problem could be that the variables %classes or @classes is not being undefined when the subroutines finish. I have tried undefining them both with no success at the beginnings and ends of each of the class_data and print_classes subroutines.

package RolePlaying::Classes; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw(class_data print_classes return_THAC0 return_famil +iarity return_non_proficiency); use lib "C:/Documents and Settings/Dawn M. Burge/My Documents/fantasy/ +files/perl/lib"; use Lingua::EN::Inflect qw(ORD); use List::Util qw(max min); use Tie::IxHash; use RolePlaying::Level qw(get_level); use RolePlaying::THAC0 qw(get_THAC0 get_familiarity get_non_proficienc +y); tie my %classes, qw(Tie::IxHash); my @classes; my @THAC0; my @familiarity; my @non_proficiency; sub class_data { my ($experience,@values) = @_; for my $class (@values) { my @class_array = split(m!\/!,$class); my $key = $class_array[0]; if (@class_array == 3) { $classes{$key}{xp_class} = $class_array[1]; $classes{$key}{class_type} = $class_array[2]; } elsif (@class_array == 2) { $classes{$key}{xp_class} = $class_array[1]; $classes{$key}{class_type} = $class_array[1]; } else { $classes{$key}{xp_class} = $class_array[0]; $classes{$key}{class_type} = $class_array[0]; } $classes{$key}{level} = get_level($classes{$key}{xp_class},$experi +ence); push @THAC0, get_THAC0($classes{$key}{level},$classes{$key}{class_ +type}); push @familiarity, get_familiarity($classes{$key}{class_type}); push @non_proficiency, get_non_proficiency($classes{$key}{class_ty +pe}); } } sub print_classes { my ($experience,@data) = @_; class_data($experience,@data); for my $key (keys %classes) { push @classes, ORD($classes{$key}{level}).' level '.$key; } return join(' / ',@classes); } sub return_THAC0 { my ($experience,@data) = @_; class_data($experience,@data); return min(@THAC0); } sub return_familiarity { my ($experience,@data) = @_; class_data($experience,@data); return min(@familiarity); } sub return_non_proficiency { my ($experience,@data) = @_; class_data($experience,@data); return min(@non_proficiency); } 1;

The loop in chracters.pl

I have also tried to undefine @clsses in the loop. However, like above, no success. The oddity is that $name and $experience do not have the same problem. They stay, I think the word is, scoped.

for my $name (keys %characters) { my $id = $name; $id =~ s! !_!g; $id =~ s![^-\w:.]!!g; my $experience = $characters{$name}{experience}; my @classes = @{$characters{$name}{classes}}; print qq{<h1 id="$id">$name</h1>\n}; print qq{\t<p>}.print_classes($experience,@classes).qq{</p>\n}; print qq{\t<p>Experience: $experience</p>\n}; }

Sample data

tie my %characters, qw(Tie::IxHash); %characters = ( "Awnday Ar'iemay" => { experience => 250000, classes => ["thief/rogue","mage/wizard"], }, "Amelda Bacoulan" => { experience => 8800, classes => ["psionisist"], }, "Sarria Bagada" => { experience => 4555, classes => ["priestess of Torm/specialty_priest/priest"], }, "Verago Brystar" => { experience => 94150, classes => ["priestess of Mystra/specialty_priest/priest","mage/wi +zard"], }, "Matea Fi'nashari" => { experience => 2500, classes => ["fighter/warrior","thief/rogue","mage/wizard"], }, );
Have a nice day!
Lady Aleena

In reply to Problem undefining a variable in a loop using a module causing a list to not empty after use of a subroutine by Lady_Aleena

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.