Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subroutine prob

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
0
0
GB
If anyone could have a quick look at this it would be appreciated. I am passing three hashes into an sub and am going to preform some action (in this case a simple print statement) if a key from the first hash exists also in the second and third hash. For somereason this isnt working although i think it works logically. If anyone has any ideas they would be welcome - heres my sub

sub combinedtable()
{
my $table1 = $_[0];
my %table1a = %$table1;
my $table2 = $_[1];
my %table2a = %$table2;
my $table3 = $_[2];
my %table3a = %$table3;


for $comp (keys (%table1a))
{
for $comp1 (keys (%table2a))
{
if($comp1==$comp)

{
for $comp2 (keys (%table3a))
{
if($comp1==$comp3){print OUT $comp3 . "\n";}
}

}
}
}

thanks
 
a little more details on what's not working, do you get an error message or does it just not print $comp3 to the file or it prints but prints the wrong value, you know what i mean?
Do you have string values in the $comp, try using "eq" instead of "=="
 

No there is no error messages the keys is getting printed out but far too many times. The key is a variable holding someones surname. I have tried using eq but nothing at all gets printed out then.
 
Sorry for somereason the value of $comp3 is in the last if statement whilst it should be $comp2. Still having the same problem though.

john
 
ok.
next thing, I don't see where you are assigning $comp3, should that be $comp2?
 
ok late response!!
do you mind posting the contents of %table1a,%table2a & %table3a
 
Not sure how your passing the hashes, but they have to be passed by reference. Then in your foreach loops you have to dereference the references. The following will print the key: "3" as it is contained in all 3 hashes.

%hash1 = ( 1 => a,
2 => b,
3 => c,
);
%hash2 = ( 4 => d,
5 => e,
3 => f,
);
%hash3 = (5 => d,
4 => e,
3 => f,
);

&combinedtable(\%hash1,\%hash2,\%hash3);

sub combinedtable() {
my ($hash1,$hash2,$hash3) = @_;
for $comp (keys (%$hash1))
{
#print "$comp\n";
for $comp1 (keys (%$hash2))
{
if($comp1==$comp)
{
for $comp2 (keys (%$hash3))
{
if($comp2==$comp){print $comp2 . "\n";}
}

}
}
}
}



 
Thanks again once more i have now solved it - i was referencing the wrongs hash and not using eq rather than ==.

Again thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top