I am fairly new to perl, so please be patient. I am trying to compare a complex hash to an array of hashes. I have been successful, however my way I believe is slow. I have tried to do it another way, but I receive a different answer. I believe it has something to do with the slice operator and $index. The following is the ugly but successful way
my $matched=0;
foreach my $id (keys %manager){
foreach my $manager (keys %{$manager{$id}}){
for my $i (0..$#{$manager{$id}{$manager}{'members'}}){
my $index=0;
foreach my $emp (@found){
if ($emp->{'ID'}=~/$manager{$id}{$manager}{'members'}[
+$i]/i){
splice(@found, $index, 1);
print $emp->{'Name'} . "\n";
$matched++;}
$index++;}}}}
Here is the other, but unsuccessful approach:
my $matched=0;
my $index=0;
foreach my $emp (@found){
DN: foreach my $id (keys %manager){
foreach my $mgr (keys %{$manager{$id}}){
for my $i(0..$#{$manager{$id}{$mgr}{'members'}}){
if ($emp->{'ID'}=~/$manager{$id}{$mgr}{'members'}[$i]/
+i){
print $emp->{'Name'} . "\n";
$matched++;
splice(@found, $index, 1);
--$index;
last DN;}}}}
$index++;}
I also have another question. I have a hash that looks something like
%hash= (
$email => {
name => 'joe'
id = > '123' })
I than push (@array, $hash{$email});
Is it than possible to get the value $email in the hash from the array?
foreach my $email (@array){
print $email->{'name'}; #ok
print $email;} #error hash refs
Thanks for any help
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.