Now, if you were using a hash reference, you would be able to change the value: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?'; }
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.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?'; }
In reply to Re: Setting variables inside a hash: newbie question
by bprew
in thread Setting variables inside a hash: newbie question
by techgirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |