Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Problem undefining a variable in a loop using a module causing a list to not empty after use of a subroutine

by 7stud (Deacon)
on Apr 07, 2010 at 04:01 UTC ( [id://833178]=note: print w/replies, xml ) Need Help??


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

have also tried to undefine @clsses in the loop. However, like above, no success.

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}};

You are declaring @classes inside the loop. Therefore, every time through the loop, the @classes variable is destroyed. A new @classes variable is created each time through the loop and it is assigned a new value.

It's not clear to me why you want to 'undefine' @classes. Is it possible you are doing something like this:

use strict; use warnings; use 5.010; my @classes = ('A', 'B'); for (1 .. 3) { my @classes = ('C', 'D'); say "@classes"; } say "\n@classes"; --output:-- C D C D C D A B

Anything you do to @classes inside the loop braces only affects the @classes variable that you declared inside the loop braces. Changes to the @classes variable inside the loop braces aren't going to affect the @classes variable declared outside the loop braces.

  • Comment on Re: Problem undefining a variable in a loop using a module causing a list to not empty after use of a subroutine
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Problem undefining a variable in a loop using a module causing a list to not empty after use of a subroutine
by amedico (Sexton) on Apr 07, 2010 at 04:18 UTC
    No, I think the OP is referring to @classes and %classes in the first file, "Classes.pm". In that package, @classes and %classes are being used as global shared state for the various functions. There, @classes and %classes need to be reset before each new call to class_data, or else the data from separate calls will commingle.

      amedico, 7stud read correctly, I was trying to undefine @classes in the for loop too. :S

      Have a nice day!
      Lady Aleena
Re^2: Problem undefining a variable in a loop using a module causing a list to not empty after use of a subroutine
by Lady_Aleena (Priest) on Apr 07, 2010 at 04:49 UTC
    every time through the loop, the @classes variable is destroyed

    I thought as much, so the problem is with Classes.pm somewhere. For some reason (%|@)classes is retaining data from previous loops. Every effort I have made to undefined both of those values has failed. I may need a whole new approach.

    Have a nice day!
    Lady Aleena

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://833178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found