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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pushing elements of Hash ?

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
I would like to know how to push a hash to use it later on (in another sub-routine). I know the push function only applies for arrays but is there a similar function for hashes ?

Here's the current coding I have.

Code:
for (keys %members) {
	if ($input{'pv'} eq $members{$_}{'region'}) {
		[b]print_members($_);[/b]
		++$NumberOfMembers;
	}
}
[code]

Instead of printing the "results" right away ([COLOR=red][b]print_members($_)[/b][/color]), I would like to push the new hash so I can use it later on in another sub-routine.

Thank you in advance.
Your help is really appreciated.
 
You'll have to declare your hash at a global level for it to work along the lines of what you've posted.

Code:
sub print_member {
  ($member)=@_;
  $member_hash{$member}=$member;
}

Would you not better off pushing onto an array, and pass the array to your print function to build up your hash?

Just a thought, rather than building up global variables

--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Thanks PaulTeg for your support

If I want to print the number of matched results ($NumberOfMembers) before printing the results... Is there a simply way to do this ?

Code:
print "$NumberOfOffices \n";

sub find_members {
	for (keys %members) {
		if ($input{'pv'} eq $members{$_}{'region'}) {
        print_members($_);
        ++$NumberOfMembers;
	    }
	}
}

&find_parents;

I can't add a return for the $NumberOfOffices string as this will not output the matched results when the find_parents sub routine will be executed. But on the other side, if I call &find_parents($NumberOfMembers) it will print the number of members but will also print the matched results (list)...

Thank you in advance
 
By the way... the print_members sub routine is working properly, so that's why I haven't posted it
 
Code:
sub find_members {
    for (keys %members) {
        if ($input{'pv'} eq $members{$_}{'region'}) {
        push (@members,$_);
        ++$NumberOfMembers;
        }
    }
    print "Number of members :\t$NumberOfMmebers\n\n";
    foreach(@members) {
       print "$_\n";
    }
    print "All memebers printed";
}

Will this work for you
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top