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))
A hash of hashes

Posted by John_Moylan (jfp), 28 January 2003
Morning all.

New to Java, and still trying to figure a few things.

I'm from a Perl background, probably the worst background to come to Java from and tend to build structures similar to the sample below.

Code:
my %catagories = (
                 'Audio' =>      {
                                         'image'    => 'pathA',
                                         'url'      => 'urlA',
                                         'text'     => 'textA',
                                         'template' => 'tplA',
                                         'short'    => 'shortA'
                                        },
                                 
                 'Computers' =>  {
                                             'image'    => 'pathB',
                                             'url'      => 'urlB,
                                             'text'     => 'textB',
                                             'template' => 'tplB',
                                             'short'    => 'shortB'
                                            },
                                 
                 'Food' =>       {
                                         'image'    => 'pathC',
                                         'url'      => 'urlC',
                                         'text'     => 'textC',
                                         'template' => 'tplC',
                                         'short'    => 'shortC'
                                       }
                 )



I would like to replicate this style in Java, probably using hashTables or the like (though some help with an arrayList of arrayLists wouldn't go amiss too as I use arrays of arrays in Perl, but hear that arrayLists are more flexible)
Or am I approaching this all wrong? perhaps I should discard any Perl'y' thoughts of structures.

sorry for the code formatting, its gone a bit strange.
Thanks
jfp


Posted by admin (Graham Ellis), 28 January 2003
There's a rough parallel between Perl's Hashes and HashMaps in Java, and between Perl's lists and ArrayLists in Java.

You'll be setting up your structures using objects, constructors and the like in Java - at first it will seem slow and longwinded but it will get easier with time, and you should find yourself able to maintain the code much more easily once you've put the initial effort in.  In other words - though I have given to a comparison don't try to do a straight conversion because Java is different

You could also use a Hashtable in place of a HashMap, and a Vector in place of an ArrayList - in both cases, these alternatives are recommended only if you want Java 1 compatability or you're writing code to be threadsafe.  A HashSet is something different - it has keys but no values, and you would use if for the type os situation where, in Perl, you set up a has and just put a "1" into the value to make it true.



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