Solved

validation

Posted on 2009-05-10
5
202 Views
Last Modified: 2012-05-06
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
Comment
Question by:cucugirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:cucugirl
ID: 24349296

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
 
LVL 84

Expert Comment

by:ozo
ID: 24349392
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
 

Author Comment

by:cucugirl
ID: 24349412
yes, I already have @table and used it in several parts in my script :) thanks! :)
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 24349456
my %table;
for( @table ){
  my($k,$v);
  ++$table{$k}{$v} while ($k,$v) = each %$_;
}
delete @hash{grep!$table{$_}{$hash{$_}},keys%hash};
0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 200 total points
ID: 24352125
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question