| |||||||||||
| |||||||||||
Dealing with a large number of dictionaries. Posted by kitwalker2886 (kitwalker2886), 4 December 2007 I m in a situation where i have a large number of dictionaries or hashes (around 30) to deal with and each having variable number of elements.. But i have to apply the same operation to all the elements of all the hashes. To do this, i tried to store all the hash names in an array and then i looped through the array and use each name to call that respective dictionary. But i got a syntax error for that. My code looked something like.. foreach $name( @array) { foreach $key (keys(%$name)) { print "$key : $$name{$key}"; } } Can i somehow modify this to make it work? or could u please suggest some other way to handle all those dictionaries without having to cut paste my code 30 times and modifying it... Posted by KevinAD (KevinAD), 4 December 2007 You need references to the hashes, not just the hash names:Code:
On an important side-note, make sure you are using the "strict" and "warnings" pragmas when writing your perl code. Posted by admin (Graham Ellis), 5 December 2007 I agree that a list of hash references is much the best way, Kevin, and I suggest that our poster goes that way. But it is possible to store just the names in a list:Code:
Which runs as: Code:
Posted by KevinAD (KevinAD), 5 December 2007 Yes, that is true, it is possible. But as you agree, the recommended method is to use hard references (as in my example) so the OP should stick with that to avoid problems that soft references can introduce.Posted by kitwalker2886 (kitwalker2886), 6 December 2007 hi ,i tried the code that graham posted and it works.. (duh!). But how is it different from the code that i posted in my original question?.. instead of using storing my hash name in a separate variable i have directly used foreach $key(keys(%$array[$i])) { ..... } Why does that make a difference?? Posted by admin (Graham Ellis), 7 December 2007 Missing curley braces to scope the variable. This works ... (extended example):Quote:
But it remains better to write the code in the manner Kvin suggested. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |