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

This problem occurs with me time and time again, how can $hashname{$this} = $that over write itself when $this and $that are always going to be unique values taken from form fields??

Replies are listed 'Best First'.
Re: Database issues
by Coplan (Pilgrim) on Apr 09, 2003 at 16:17 UTC
    Is $this a constant? If it isn't, than there's your answer.

    Keep in mind, if you're using a key that is a variable, it will change at any given point throughout the program as you redefine what $this is. When you call it, you can really only say it's getting over-written if $this is constant and never changing.

    #/!usr/bin/perl use strict; my $this = "key1"; my $that = "in1"; $hashname{$this} = $that; # $hashname{'key1'} is 'in1' $this = "key2"; $that = "in2"; $hashname{$this} = $that; # $hashname{'key2'} is 'in2' # $hashname{'key1'} is still 'in1' # eof
    Also note that if you're doing some test down the line, if $that is a string, make sure you're using the text operators (eq, for example) as opposed to the numerical operators (==, !=, etc).

    Without a better description of your problem, or at least a code snippet, I'm not sure you're going to get the 100% answer you're looking for.

    --Coplan

      Snippet:
      # have a db and hash $stats open # form is incomplete in the snippet but you get the general idea # print "Name: " textfield( -name => 'name', -size => 100); print "age: " textfield( -name => 'name', -size => 5); my $name = param('name'); my $age = param('age'); #store every person from the form into the database $stats{$name} = $age;
        ... print "age: " textfield( -name => 'age', -size => 5); ...

        -derby

        Regardless of what you print beside it the form item has a value called name. You set them both to be 'name'. When you submit the form with

        Name: [ george ] Age: [ 6 ]


        when using a GET type request, note that the cgi will be appended with ?name=george&name=6.

        This describes the parameters, and in fact, the outcome is that there is no parameter named 'age'. your fix is this:
        print "age: " textfield ( -name => 'age', -size => 5);


        -nuffin
        zz zZ Z Z #!perl
Re: Database issues
by nothingmuch (Priest) on Apr 09, 2003 at 16:13 UTC
    I assume you are referring to some DBM tied hash. What do you mean how can it overwrite itself?

    A possible scenario i can think up:
    You have dbm with some data in it. the $key this is some data, and it's value $that are stored beside some other values in the hash.
    You want to delete the old value of $this, the one from the previous form submit, and create a new one relevant to the new $this?

    The simplest way to do this is to store an extra key in the hash, a constant one named 'current_this' or whatnot, with the value of $this, to be used as the new key. But this is a bit backwards to start with. Perhaps you should rethink your implementation?

    -nuffin
    zz zZ Z Z #!perl
      What I'm trying to do is get store $name and $age from a form into a database. So I try using $stats{$name} = $age; thinking that would give store a list like (see below) for me. What am I doing wrong?

      George Clinton 10000 Bill Bush 9999999 Pocahontas 20 Godzilla 123
        foreach $name (qw(George Bill Pocahantas Godzilla)){ $stats{$name} = int rand(10); # give the name a random value } while(my ($key,$value) = each %stats){ print "$key is $value years old\n"; }
        Does this help?

        -nuffin
        zz zZ Z Z #!perl