Solved

validation

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

808 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