Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Setting variables inside a hash: newbie question

by techgirl (Beadle)
on Nov 14, 2004 at 22:44 UTC ( [id://407720]=perlquestion: print w/replies, xml ) Need Help??

techgirl has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a natural language processing program to take in responses from a user and give responses back.

I would like to set up a data structure in which I initialize the questions my bot will ask at the beginning of the program. I'm using a hash mapped to different questions.

Is there a way to use another variable within the definitions of these hash values? I'm finding that since I'm initializing the hash at the beginning, if I update a variable value later on, it will not pick up the changed value. I guess I need a pointer, rather than a flat variable? I don't really understand the section on hard vs symbolic references in Ch. 8 of 'learning perl', or am absolutely sure if this is the right direction...

Is there a special way I should be referencing my variable to make it look up the value for it (when I output it, which would be most useful), or do I need to refresh all the initializations each time I get new user input?

If this is unclear, pls let me know; I can post the code...

Thanks in advance,
techgirl

  • Comment on Setting variables inside a hash: newbie question

Replies are listed 'Best First'.
Re: Setting variables inside a hash: newbie question
by bprew (Monk) on Nov 14, 2004 at 23:30 UTC
    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
      Ben is absolutely right - pass a hash *by reference* - that is, pass a reference to a hash instead of the hash itself.

      One concept that Ben didn't mention that I think is worth acknowledging is that when parameters are passed to a subroutine, that argument list is "flattened" out into a list of scalars - arrays are flattened out into a list of scalars, and hashes are flattened out into a list of scalars - the result is one big list of scalars getting passed to the subroutine. This isn't a problem unless you pass an array or hash first, and *then* other parameter(s) - the array or hash that receives the 1st argument in the subroutine will likely "swallow up" all the arguments being passed in, since it can't tell from the one big "list" of scalars coming in which ones are intended for it and which ones are intended for other parameters.

      To avoid confusion when passing "things" to subroutines, pass each parameter as a scalar - this means

      * passing scalars as themselves * passing a "reference to an array" (which is itself a scalar) instead of the array itself * passing a "reference to a hash" (which is itself a scalar) instead of the hash itself
      Then if you really want to work with a "copy" of the array or hash (instead of just using the reference to refer to elements in the array or hash) whose reference was passed into the subroutine, you can create a copy like this:
      sub do_something($my_scalar, $my_arrayref, $my_hashref) { my @my_array = @$my_arrayref; my %my_hash = %$my_hashref; ### more code here ### }
      HTH.
Re: Setting variables inside a hash: newbie question
by revdiablo (Prior) on Nov 14, 2004 at 23:11 UTC
    if I update a variable value later on, it will not pick up the changed value

    You need to store a reference, rather than simply copying the value. Example:

    my $foo = "foo"; my %hash = (foo => \$foo); print ${$hash{foo}}; $foo = "bar"; print ${$hash{foo}};
Re: Setting variables inside a hash: newbie question
by ysth (Canon) on Nov 15, 2004 at 02:46 UTC
    Ignore symbolic references. They won't do anything for you that hard references won't. (There are some exceptional cases, but not anything you should be worrying about.) Perhaps take a tour through perlreftut?
Re: Setting variables inside a hash: newbie question
by BUU (Prior) on Nov 15, 2004 at 01:37 UTC
    It sounds more like you want a templating engine. You have the basic "template" of your sentence and then you fill in the specific variables from user input, right? Well you can build a simple templater something like:
    s/\$(\w+)/$values{$1}/g
    Where $_ contains the template string and %values contain your key-value substitution pairs. Then anything that looks like $foo gets replaced with the value corresponding to that key in %values. Of course this is just a rudimentaty templating "engine" and probably has a few flaws, so you probably want to use a prebuilt module, such as Text::Template or HTML::Template
Re: Setting variables inside a hash: newbie question
by data64 (Chaplain) on Nov 15, 2004 at 06:59 UTC

    I think others have answered your question but I would like to add that any time I find myself passing a hash (or hash reference) around to a whole bunch of functions that manipulate the hash, it starts becoming obvious that I should think about converting that hash into a class and the all those functions into methods of that class. This does not work all the time and for all the functions but most of the time on a majority of the functions, at least for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://407720]
Approved by rozallin
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-14 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found