In that instance, $vars is referencing the hash %vars (as is is followed by
{vNameL}). Brett was not using the same variable names as you were in your
code, but simplified ones to explain the issue without clouding it in long
and complicated variable names. Less typing too ;)


What is this ...

---
        sub ShowCriteria { #($theKey, $theValue, $theName) {
                $arr_criteria_hash{@_[0]} = @_[1];
...
        }
---

All about?? You won't get blank keys. You don't need to create the hash
before you use it, Perl will handle that. You could do it like this

$value = 50;
$hash{"key"} = $value;
$hash{"key2"} = $value;

That will give you a hash which looks like this

%hash = (
        "key" => 50,
        "key2" => 50,
);

To read the values back out of the hash

foreach $key (sort keys %hash) { # The sort will return the keys in the hash
sorted 
        print "$key => $hash{$key}\n";
}


You don't need a while loop to read it.

Using your code, you could do something like

foreach $field (keys %arr_criteria_hash) {
        $newText = " $field like '%$hash{$field}%'";
        ...
        }

But if what you've got is working...

John

-----Original Message-----
From: Bradshaw, Brian [mailto:[EMAIL PROTECTED]]
Sent: 17 September 2001 17:07
To: [EMAIL PROTECTED]
Subject: RE: hash assignment question


This did it. I defined the hash and then did the key/value assignments like
this:

%arr_criteria_hash = ();

        sub ShowCriteria { #($theKey, $theValue, $theName) {
                $arr_criteria_hash{@_[0]} = @_[1];
...
        }
I did this so I can read it with a while loop. I didn't want blank keys if I
could help it.

So I have a nice, neat:
while (($field, $search) = each(%arr_criteria_hash))
        {
                $newText = " $field like '%$search%'";
...
        }
Without checking for decoys.
Thanks for the response. And thanks to John E.

However, in "if($vars{vNameL}) {" what does the $vars do? I have not seen
that.

-----Original Message-----
From: Brett W. McCoy [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 17, 2001 11:04 AM
To: Bradshaw, Brian
Cc: [EMAIL PROTECTED]
Subject: Re: hash assignment question


The way you are doing it, you are redefining your hash each time, not
adding new key/value pairs.  I would create hash first (outside your if
statements), and then add elements as you need them:

my %arr_criteria = ();

...

$arr_criteria{'last_name'} = $vNameL;

Perl will automatically create the hash element for you.

In fact, as far as organization goes, I would add the elements to the hash
initially, with sensible default values (even empty strings) and then test
to see if the hash element exists.  Are you using CGI.pm?  You can grab
ALL of the values from a form by using the Vars() function, which will put
everything nice and neat into a hash for you.  Then you can do:

if($vars{vNameL}) {

    # do stuff
}

And not even worry about adding the elements yourself.

-- Brett

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to