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!

annoying problem - extracting names from hashes

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
0
0
GB
Hi

I have got an awkward problem which i was wondering if anyone coud help me with. I have got a bunch of hashes that i am passing into function to produce html tables. The problem is that i want the html header to math the name of the hash taken as input so if i pass in %example1 then i want the title to be example1 - if i input %example34 then i want the title to be example34. Is there a way of extracting this information out of the hash and storing it i a variable.

cheers folks
 
Determined it was possible, I came up with this:
Code:
our %example1  = ( key => 'one!' );
our %example34 = ( key => 'three four!' );
asdf(*example1);
asdf(*example34);

sub asdf
{
	my $name = *{$_[0]}{NAME};
	my %hash = %{*{$_[0]}{HASH}};
	print "Name: $name\nHash{key}: $hash{key}\n";
}
Now, that said, it's ugly and there's got to be a better way to do it. First off, since it's symbol table fun, you can't have %example21 declared with 'my', it must be 'our', listed in 'use vars()', or if you're not running under strict, just use it. Notice how it's called. I couldn't figure out a way to get a name from a hashref (makes sense), so you have to pass a typeglob (or a reference to).

Done with the ugly, now onto how you can do it without. Really, you should just pass the name of the hash as another parameter to the sub (ie, one that specifies the title of the generated page, not a bad thing to have a parameter for), or maybe even make a data structure out of it all (a hash containing each hash) and pass in the name of which hash to use (see perldsc if you want to try this method).

Anyway, the bottom line is if you have to resort to tinkering with the symbol table, there's generally a better way to do it.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top