I am accepting some input values via a web form.  I want to compare their
values to values in the database and then update those that are different.

I am using hashes to hold the values.  Here's what we've got:

#values sucked in from a form:
$params = {
        report_1 => 'yes',
        report_2 => 'no',
        report_3 => 'yes',
        report_4 => 'yes'
};


#values from the database:
%compare = (
        1 => 'yes',
        2 => 'no',
        3 => 'no',
        4 => 'no'
);

(don't ask why the key names are different -- too complicated to explain
:)

#hash to store values to be inserted into database:
my %update;

So I want to have an algorithm that will compare these two hashes and
extract out the values that are different, for updating.  I know I can use
for/foreach loops to accomplish this.  But I wanted something quicker and
more succinct.  Here's what I have:

%update = map { ($_, $params->{"report_$_"}) }
          grep { s/report_(\d+)/$1/ and
          ($params->{"report_$_"} ne $compare{$_}) }
          keys %$params;

Now, I know this works, but I was wondering if there is a better way to do
it, or maybe a more Perl-ish way.  (I reduced it to this from two separate
foreach loops).

Chris





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

Reply via email to