Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

validation

how can i check that both key/value pairs in a hash:
my %hash = {'TEST'=>'T,'QUIZ'=>'1','HWORK'=>'3'};

are in the following element:
      my @table = ( {'TEST'=>'T'}, {'TEST'=>'F'},{'QUIZ'=>'1'},{'QUIZ'=> '2'},{'HWORK'=>'3'}, {'HWORK'=>'3'});

if they're not then i'd like to delete them by making a copy of the original hash. For example:
my %hash = {'TEST'=>'T,'QUIZ'=>'4','HWORK'=>'3'}; and since 'QUIZ'=>'4' is not in @table then I would have my %hash or a copy of my %hash =  {'TEST'=>'T,,'HWORK'=>'3'};


example:
0
cucugirl
Asked:
cucugirl
  • 2
  • 2
2 Solutions
 
cucugirlAuthor Commented:

a small correction:

my @table = ( {'TEST'=>'T'}, {'TEST'=>'F'},{'QUIZ'=>'1'},{'QUIZ'=> '2'},{'HWORK'=>'3'}, {'HWORK'=>'4'});
my %hash =  {'TEST'=>'T','HWORK'=>'3'};
thankssss in advance! :)
0
 
ozoCommented:
Is there a reason that you are not using
 my %table = (TEST=>{T=>1,F=>1},QUIZ=>{1=>1,2=>1},HWORK=>{3=>1,4=>1});
0
 
cucugirlAuthor Commented:
yes, I already have @table and used it in several parts in my script :) thanks! :)
0
 
ozoCommented:
my %table;
for( @table ){
  my($k,$v);
  ++$table{$k}{$v} while ($k,$v) = each %$_;
}
delete @hash{grep!$table{$_}{$hash{$_}},keys%hash};
0
 
vikaskhoriaCommented:
Check my script below:
(This deletes elements from the hash and the original hash is modified to the desired hash)

Thanks,
Vikas.
#!/usr/bin/perl
 
my %my_hash = ('TEST' =>'T' ,'QUIZ'=>'4','HWORK'=>'3');
my @table = ( {'TEST'=>'T'}, {'TEST'=>'F'},{'QUIZ'=>'1'},{'QUIZ'=> '2'},{'HWORK'=>'3'}, {'HWORK'=>'3'});
 
for my $value (keys %my_hash)
{
        my $found = 0;
        foreach $i (0 .. @table-1)
        {
                if ( $table[$i]{$value} == $my_hash{$value} ) {
                        $found = 1;
                }
        }
        if ( ! $found )
        {
                delete($my_hash{$value});
        }
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now