Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
Multi-level list generation
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Steve, post: 4875051"] OK, here: 1) tested 2) compiles 3) works #! /usr/bin/perl use warnings; use strict; my %one = ( onek1v1 => 'hash one value 1', onek2v1 => 'hash one value 2', ); my %oneref = ( onerefk1v1 => { anotherlevel => 'hash one down two' }, onerefval => 'hash one value down one', ); $one{'downone'} = \%oneref; my %two = ( twok1v1 => 'hash two value 1', twok2v1 => 'hash two value 2', ); my %three = ( threek1v1 => 'hash three value 1', threek2v1 => 'hash three value 2', ); my %main_hash = ( one => \%one, two => \%two, three => \%three, ); recurse_hash( \%main_hash ); our $spct = 0; ## track leading spaces count sub recurse_hash { my $refhash = shift; $refhash or return ''; $spct and print ' ' x $spct; # spacecount x spacespace print "<ul>\n"; $spct += 1; for( keys %{$refhash} ){ if( ref $refhash->{$_} eq 'HASH' ){ $spct += 1; recurse_hash( $refhash->{$_} ); $spct -= 1; } else{ $spct+= 1; print ' ' x $spct; print "<li>$refhash->{$_}</li>\n"; $spct -= 1; } } $spct -= 1; print ' ' x $spct; print "</ul>\n"; } exit; Command line Output: <ul> <ul> <li>hash three value 1</li> <li>hash three value 2</li> </ul> <ul> <li>hash one value 2</li> <ul> <li>hash one value down one</li> <ul> <li>hash one down two</li> </ul> </ul> <li>hash one value 1</li> </ul> <ul> <li>hash two value 2</li> <li>hash two value 1</li> </ul> </ul> You'll note the order is NOT preserved as I'm not using Tie::IxHash. Anyway, play with the script portion to get a feel for what is going on. Add/Remove hash refs, whatever. Once the concept starts sinking in you will have taken a major step in understanding/using Perl. I read somewhere once that if you are not using hashes you are not doing Perl. Spot on. \s [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
Multi-level list generation
Top