Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-18 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found