Solved

validation

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now