Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Associated Array Appending Unwillingly

by cgi_pro (Initiate)
on Nov 16, 2005 at 05:25 UTC ( #508918=perlquestion: print w/replies, xml ) Need Help??
cgi_pro has asked for the wisdom of the Perl Monks concerning the following question:


I have a subroutine that returns a string and an associated array. When I call it, I do it as in the following:

my ($String1 %AssocArray1) = MySubRoutine(SOME_ARGUMENTS1);

then I print the %AssocArray1 to the screen as follows:

foreach $test(keys %AssocArray1){ print "$test\n"; }

everything looks fine now, then when I call my subroutine again as follows:

my ($String2, %AssocArray2) = MySubRoutine(SOME_ARGUMENTS2);

and I print it to the screen as follows:

foreach $test(keys %AssocArray2){ print "$test\n"; }

it prints to me both %AssocArray1 (that I already printed before) then %AssocArray2

I tried to delete all elements of %AssocArray1 before I even call my subroutine for the second time for %AssocArray2, and then printed %AssocArray2 to the screen, but once again the same thing happened, it keeps printing %AssocArray1 and %AssocArray2 when I only want to print %AssocArray2

Anyone knows what's wrong with my coding here?


I just checked my subroutine and CountOrlok you were right, I don't know how I missed defining the associated array for local use only. Thanks a lot to you and for the rest of the guys who were trying to help as well :) My program is now working perfectly as expected :)

Edited by planetscape - added code tags and fixed formatting

Replies are listed 'Best First'.
Re: Associated Array Appending Unwillingly
by CountOrlok (Friar) on Nov 16, 2005 at 05:55 UTC
    I am guessing that the hash variable that you are passing back from inside MySubRoutine() is a global variable. Define it as a local within the subroutine using "my" instead and see what happens.


Re: Associated Array Appending Unwillingly
by Roger (Parson) on Nov 16, 2005 at 05:43 UTC
    It appears to me that your subroutine 'MySubRoutine' might have a bug in its implementation. Can you include the code of your subroutine so we can have a complete picture of your code?
Re: Associated Array Appending Unwillingly
by jbrugger (Parson) on Nov 16, 2005 at 05:43 UTC
    I don't know what happens inside your subroutine or in your script since you don't provide a lot of information.
    Anyway, i do think, you probably messed around with the scope of your variables together with the use of references.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Associated Array Appending Unwillingly
by demerphq (Chancellor) on Nov 16, 2005 at 10:11 UTC

    Hi. Please read the Writeup Formatting Tips. You should use code tags for your code, and IMO you should ditch the font coloring. If you visit Display Settings you will see that users can view the site in a variety of color schemes, and can even supply their own CSS for rendering the site just as they like. The HTML tricks that you have used might look ok for some users, but for many others it will be totally unreadable.


Re: Associated Array Appending Unwillingly
by blazar (Canon) on Nov 16, 2005 at 10:16 UTC

    First of all, a little bit of terminology: the "object" you're referring to is called an associative array. Not "associated". They're also commonly called hashes because their implementation is based on the concept of a hashing function.

    Said this, if what is happening what both I and everyone else thus far are guessing, i.e. that you failed to properly declare a certain hash as lexically scoped to your sub, then it is worth repeating the single most precious piece of advice we usually give to Perl newbies:

    use strict; use warnings;
    Maybe you already do. Maybe you don't and chances are that this is the case, taking into account your post.

    To be sure, let me rephrase the above advice in these terms: "let perl do all that it can to help you avoid trivial mistakes".

    Incidentally, use code tags just like quite anybody else for your pieces of code rather than ad-hoc visually disturbing (IMHO) formatting.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://508918]
Approved by jbrugger
[1nickt]: Discipulus Sorry had no time before. ++Test::Warn, but also simply Capture::Tiny to capture everything and examine it. Or just Test::Exception:: dies_ok().

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-01-22 13:09 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.