It sounds like you're passing around a hash to various subroutines, when you want to pass a hash-reference. Passing a reference to a hash allows you to change the hash in a subroutine and have that change affect the rest of accesses to that hash. As an example:
my %regular_hash = ( question1 => 'How are you?', question2 => 'What is your name', ); print $regular_hash{question1}; # prints 'How are you?' change_question1(%regular_hash); print $regular_hash{question1}; # still prints 'How are you?' sub change_question1 { my %regular_hash = @_; $regular_hash{question1} = 'What is your mothers name?'; }
Now, if you were using a hash reference, you would be able to change the value:
my $hash_rev = { #notice the change of opening brace question1 => 'How are you?', question2 => 'What is your name', }; # and the corresponding close # and slightly different access methods print $hash_ref->{question1}; # prints 'How are you?' change_question1($hash_ref); print $hash_ref->{question1}; # now prints 'What is your mothers name? +' sub change_question1 { my $hash_ref = shift @_; $hash_ref->{question1} = 'What is your mothers name?'; }
The difference is that Perl will pass its arguments by value, which means that it will create a copy of the data that you passed. However, when you pass a reference to a data-type, Perl passes the a memory location (similar to a pointer in C/C++). That memory location is a container for the values. So, when you change a value in the original container, it changes it everywhere. If that's not what you're doing, perhaps the code you're working with would help.

--
Ben

In reply to Re: Setting variables inside a hash: newbie question by bprew
in thread Setting variables inside a hash: newbie question by techgirl

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.