Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:
my %foo = (1..10);
my %bar = (11..20);
my @hashes = (\%foo, \%bar);
foreach my $hashref (@hashes) {
  foreach my $key (keys %{$hashref}) {
     print "$key = $hashref->{$key}\n";
  }
}


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:
%first = ("Graham" => "Melksham", "Chris" => "Beach", "Martin" => "Melksham");
%second = ("Lisa" => "Melksham", "Leah" => "Melksham");

@lohash = ("first","second");

foreach $hname(@lohash) {
       %hd = %$hname;
       print %hd,"\n";
       }


Which runs as:

Code:
Dorothy:dec07 grahamellis$ perl sl
MartinMelkshamChrisBeachGrahamMelksham
LisaMelkshamLeahMelksham
Dorothy:dec07




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:
%first = ("Graham" => "Melksham", "Chris" => "Beach", "Martin" => "Melksham");
%second = ("Lisa" => "Melksham", "Leah" => "Melksham");

@lohash = ("first","second");

foreach $hname(@lohash) {
  %hd = %$hname;
  print %hd,"\n";
  }

for ($i=0; $i<=$#lohash; $i++) {
foreach $key(keys(%{$lohash[$i]})) {
       print $i,$key,"\n";
       }}


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho