Solved

validation

Posted on 2009-05-10
5
185 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
  • 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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