[31830] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3093 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 22 14:14:34 2010

Date: Sun, 22 Aug 2010 11:14:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 22 Aug 2010     Volume: 11 Number: 3093

Today's topics:
    Re: Multi-level list generation <tuxedo@mailinator.com>
    Re: Multi-level list generation <tuxedo@mailinator.com>
    Re: Multi-level list generation <tuxedo@mailinator.com>
    Re: Multi-level list generation <uri@StemSystems.com>
    Re: Multi-level list generation sln@netherlands.com
    Re: Multi-level list generation <tuxedo@mailinator.com>
    Re: Multi-level list generation <stevem_@nogood.com>
    Re: Multi-level list generation <stevem_@nogood.com>
    Re: Multi-level list generation <stevem_@nogood.com>
    Re: Multi-level list generation <tadmc@seesig.invalid>
    Re: regular expression for beow text <hara.acharya@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 22 Aug 2010 14:19:46 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Multi-level list generation
Message-Id: <i4r4l2$fir$03$1@news.t-online.com>

Steve wrote:

> On 08/21/2010 03:32 PM, Tuxedo wrote:
> > Steve wrote:
> >
> >> On 08/21/2010 02:04 PM, Tuxedo wrote:
> >>> I have a question about how to generate a multi-level (nested) list
> >>> structure by perl. I currently have a 2-level
> >>> <ul><li><ul><li></li></ul></li></ul>   structure produced via a perl
> >>> script, which works fine for its purpose. An example HTML output by
> >>> the existing script is:
> >>>
> >>> <ul>
> >>>      <li><a href=subject_1.0.html>Subject 1.0</a>
> >>>         <ul>
> >>>            <li><a href=page_1.1.html>Page 1.1</a></li>
> >>>            <li><a href=page_1.2.html>Page 1.2</a></li>
> >>>            <li><a href=page_1.3.html>Page 1.3</a></li>
> >>>        </ul>
> >>>      </li>
> >>> </ul>
> >>>
> >>> There's more non-relevant code that I've stripped for a bit of
> >>> clarity, such as CSS etc. In fact, the actual HTML code is largely
> >>> irrelavant. Anyway, a barebone version of the perl procedure
> >>> generating the above is:
> >>>
> >>> #!/usr/bin/perl -w
> >>>
> >>> use Tie::IxHash;
> >>> use strict;
> >>> use warnings;
> >>>
> >>> my $object1 = tie my %listoflinks, "Tie::IxHash";
> >>>
> >>> %listoflinks = ('subject_1.0.html', =>   'Subject 1.0',
> >>>                   'page1.1.html', =>   'Page 1.1',
> >>>                   'page1.2.html', =>   'Page 1.2',
> >>>                   'page1.3.html', =>   'Page 1.3');
> >>>
> >>> for (\%listoflinks) {
> >>> my $firstkey = each %$_;
> >>>
> >>> print "<ul>\n"; # open 1st UL
> >>>
> >>> print "<li><a href=$firstkey>$listoflinks{$firstkey}</a>\n"; # open
> >>> 1st LI
> >>>
> >>> print "<ul>\n"; # open nested UL
> >>>
> >>> while ( local $_ = each %$_ ) {
> >>>      { print "<li><a href=$_>$listoflinks{$_}</a></li>   \n" } # print
> >>>      some LI's }
> >>>
> >>> print "</ul>\n"; # close nested UL
> >>> print "</li>\n"; # close first LI
> >>> print "</ul>\n"; # close first UL
> >>>
> >>> }
> >>>
> >>> The above procedure was put together with good help from this group
> >>> ages ago. As mentioned, the code takes care of the 2-level list
> >>> structure and does so by fetching the $firstkey from the array entries
> >>> or LoH and inserting the needed opening and closing UL's and LI's in
> >>> the right places.
> >>>
> >>> However, I'm not quite sure how to change the script to generate a
> >>> third level, such as:
> >>>
> >>> <ul>
> >>>      <li><a href=subject_1.0.html>Subject 1.0</a>
> >>>         <ul>
> >>>            <li><a href=page_1.1.html>Page 1.1</a></li>
> >>>            <li><a href=page_1.2.html>Page 1.2</a></li>
> >>>            <li><a href=page_1.3.html>Page 1.3</a></li>
> >>>            <li><a href=subject_2.0.html>Subject 2.0</a>
> >>>               <ul>
> >>>                  <li><a href=page_2.1.html>Page 2.1</a></li>
> >>>                  <li><a href=page_2.1.html>Page 2.2</a></li>
> >>>               </ul>
> >>>            </li>
> >>>        </ul>
> >>>      </li>
> >>> </ul>
> >>>
> >>> Or for example, the same structure, with another two second-level list
> >>> items at the end:
> >>>
> >>> <ul>
> >>>      <li><a href=subject_1.0.html>Subject 1.0</a>
> >>>         <ul>
> >>>            <li><a href=page_1.1.html>Page 1.1</a></li>
> >>>            <li><a href=page_1.2.html>Page 1.2</a></li>
> >>>            <li><a href=page_1.3.html>Page 1.3</a></li>
> >>>            <li><a href=subject_2.0.html>Subject 2.0</a>
> >>>               <ul>
> >>>                  <li><a href=page_2.1.html>Page 2.1</a></li>
> >>>                  <li><a href=page_2.1.html>Page 2.2</a></li>
> >>>               </ul>
> >>>            </li>
> >>>            <li><a href=page_1.4.html>Page 1.4</a></li>
> >>>            <li><a href=page_1.5.html>Page 1.5</a></li>
> >>>        </ul>
> >>>      </li>
> >>> </ul>
> >>>
> >>> Naturally a different array structure would be required in my
> >>> %listoflinks to output the above. Any advise or examples how this may
> >>> be pieced together would be most helpful.
> >>>
> >>> Perhaps someone has a procedure in use that does something similar
> >>> already?
> >>>
> >>> Many thanks,
> >>> Tuxedo
> >>>
> >>> NB: System load efficiency is not an issue, as the procedure will run
> >>> only occasionally on a local machine to generate HTML sent onto a web
> >>> server in static format. In other words, the script will not run
> >>> against any real web page requests. The procedure is simply meant to
> >>> be an easy maintenance tool.
> >>>
> >>>
> >>
> >> I use recursive routines a *lot* in printing out nested data
> >> structures, and they are your friend in cases like this...
> >>
> >> The below is:
> >> 1) not tested in any way,
> >> 2) may not even compile,
> >> 3) and is just a concept.
> >>
> >> %hash = ( whatever, too lazy to make one );
> >>
> >> recurse_hash( \%hash );
> >>
> >> sub recurse_hash {
> >>       my $refhash = shift;
> >>       $refhash or return '';
> >>
> >>       print "<ul>\n";
> >>
> >>       while( keys %{$refhash} ){
> >>           if( ref $refhash->{$_} eq 'HASH' ){
> >>               recurse_hash( $refhash->{$_} );
> >>           }
> >>           else{
> >>               print "<li>$refhash->{$_}</li>\n";
> >>           }
> >>       }
> >>
> >>       print "</ul>\n";
> >> }
> >>
> >> The beauty of a recursive is it flat doesn't matter how many levels
> >> deep the data structure is.
> >>
> >> The downside is it flat doesn't matter how many levels deep the
> >> recursive 'thinks' the data structure is and sloppy programming can
> >> bite you big time..... infinite recursion anyone?
> >>
> >> hth,
> >>
> >> \s
> >>
> >>
> >
> > Thanks for the above solution! However, it is a bit difficult for me to
> > figure exactly how the %hash = (part may be composed) to generate a
> > multi-level list structure. Any additional pointers anyone?
> >
> > Tuxedo
> >
> >
> >
> 
> Well.....
> 
> my %hash = (
>      key1 => { subkey => 'value',
>                subhash => {
>                            subsubkey1 => 'value',
>                            subsubkey2 => 'another value',
>                           },
>              },
>      key2 => 'value',
>      etc  => {
>              },
> 
> );
> 
> But, 'more better' would be to go a reliable (and vetted) source like:
> 
> http://perldoc.perl.org/perldsc.html
> 
> hth,
> 
> \s


Thanks for the hash example and perldoc resource. However, it's still a bit 
confusing. To gain a bit further understanding I tried to run your script 
'as is' but it failed to compile, as of course you warned me it may do. In 
saving your script as a file named testrun.pl and running it, the following 
errors are repeatedly returned to the shell until I hit Ctrl+C:

<li></li>
Use of uninitialized value in hash element at ./testrun.pl line 27.
Use of uninitialized value in hash element at ./testrun.pl line 31.
Use of uninitialized value in concatenation (.) or string at ./testrun.pl 
line 31.
<li></li>
Use of uninitialized value in hash element at ./testrun.pl line 27.
Use of uninitialized value in hash element at ./testrun.pl line 31.
Use of uninitialized value in concatenation (.) or string at ./testrun.pl 
line 31.
<li></li>

etc.

Unfortunately I do not understand the meaning of the above errors and if I 
redirect the output to a file, like in ./testrun.pl > file.txt, the file 
contains a long list of empty <li></li> containers after an opening <ul>:

<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>

etc...

Below is the exact copy of what I tried to run:

#!/usr/bin/perl -w

use strict;
use warnings;

my %hash = (
     key1 => { subkey => 'value',
               subhash => {
                           subsubkey1 => 'value',
                           subsubkey2 => 'another value',
                          },
             },
     key2 => 'value',

); 


recurse_hash( \%hash );

sub recurse_hash {
     my $refhash = shift;
     $refhash or return '';

     print "<ul>\n";

     while( keys %{$refhash} ){
         if( ref $refhash->{$_} eq 'HASH' ){
             recurse_hash( $refhash->{$_} );
         }
         else{
             print "<li>$refhash->{$_}</li>\n";
         }
     }

     print "</ul>\n";
}

Anyone knows where the error(s) in the above procedure are? 

Thanks again,
Tuxedo





------------------------------

Date: Sun, 22 Aug 2010 17:11:42 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Multi-level list generation
Message-Id: <i4rene$elu$00$1@news.t-online.com>

ccc31807 wrote:

> On Aug 21, 3:04 pm, Tuxedo <tux...@mailinator.com> wrote:
> > I have a question about how to generate a multi-level (nested) list
> > structure by perl. I currently have a 2-level
> > <ul><li><ul><li></li></ul></li></ul> structure produced via a perl
> > script, which works fine for its purpose. An example HTML output by the
> > existing script is:
> 
> Mark Jason Dominus, in 'Higher Order Perl' has a section on parsing
> HTML in chapter 1. You can download the book over the internet, but
> it's well worth buying, and I would encourage you to do so.
> 
> CC

Thanks for the tip. Seems to be a good book!

Tuxedo


------------------------------

Date: Sun, 22 Aug 2010 17:17:21 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Multi-level list generation
Message-Id: <i4rf21$l9j$02$1@news.t-online.com>

Uri Guttman wrote:

> >>>>> "c" == ccc31807  <cartercc@gmail.com> writes:
> 
>   c> On Aug 21, 3:04 pm, Tuxedo <tux...@mailinator.com> wrote:
>   >> I have a question about how to generate a multi-level (nested) list
>   >> structure by perl. I currently have a 2-level
>   >> <ul><li><ul><li></li></ul></li></ul> structure produced via a perl
>   >> script, which works fine for its purpose. An example HTML output by
>   >> the existing script is:
> 
>   c> Mark Jason Dominus, in 'Higher Order Perl' has a section on parsing
>   c> HTML in chapter 1. You can download the book over the internet, but
>   c> it's well worth buying, and I would encourage you to do so.
> 
> the OP isn't parsing but generating html. parsing it should be done with
> a module. generating it is done well with templates but he still needs
> to learn data structures to work with them. regardless of the
> technology, nested html needs nested data which means perl data
> structures. they are used in some many perl programs that they are
> critical to learn early one. perlreftut, perldsc and perllol are
> required reading from the perl docs.
> 
> uri
> 

Will look into perlreftut, perldsc and perllol. My task may seem trivial, 
but as you say, the relevant data structures are needed, which makes this 
one a hard nut to crack without a fairly deep level of perl knowledge.

Tuxedo


------------------------------

Date: Sun, 22 Aug 2010 12:13:45 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Multi-level list generation
Message-Id: <87lj7ypqva.fsf@quad.sysarch.com>

>>>>> "T" == Tuxedo  <tuxedo@mailinator.com> writes:

  T> Uri Guttman wrote:
  >> 
  >> the OP isn't parsing but generating html. parsing it should be done with
  >> a module. generating it is done well with templates but he still needs
  >> to learn data structures to work with them. regardless of the
  >> technology, nested html needs nested data which means perl data
  >> structures. they are used in some many perl programs that they are
  >> critical to learn early one. perlreftut, perldsc and perllol are
  >> required reading from the perl docs.

  T> Will look into perlreftut, perldsc and perllol. My task may seem trivial, 
  T> but as you say, the relevant data structures are needed, which makes this 
  T> one a hard nut to crack without a fairly deep level of perl knowledge.

i consider perl refs and data structure mid-level perl and not deep
knowledge. they aren't that hard to learn and they are used all the time
which make them important to learn.

as for templating html, there are many choices. i, of course, recommend
Template::Simple which you can learn quickly and will help in this
task. regardless of the method you need to learn perl data structures.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Sun, 22 Aug 2010 09:30:38 -0700
From: sln@netherlands.com
Subject: Re: Multi-level list generation
Message-Id: <fej276p07f8qljdu13uevhrv2nnu64vcdv@4ax.com>

On Sat, 21 Aug 2010 21:04:28 +0200, Tuxedo <tuxedo@mailinator.com> wrote:

>I have a question about how to generate a multi-level (nested) list 
>structure by perl. I currently have a 2-level
><ul><li><ul><li></li></ul></li></ul> structure produced via a perl script, 
>which works fine for its purpose. An example HTML output by the existing 
>script is:
>
><ul>
>   <li><a href=subject_1.0.html>Subject 1.0</a>
>      <ul>
>         <li><a href=page_1.1.html>Page 1.1</a></li> 
>         <li><a href=page_1.2.html>Page 1.2</a></li> 
>         <li><a href=page_1.3.html>Page 1.3</a></li> 
>     </ul>
>   </li>
></ul>
>

I don't have Tie::IxHash and it is not part of the core.
After commenting it out and running the code, it prints:

<ul>
<li><a href=page1.3.html>Page 1.3</a>
<ul>
<li><a href=subject_1.0.html>Subject 1.0</a></li>
<li><a href=page1.2.html>Page 1.2</a></li>
<li><a href=page1.1.html>Page 1.1</a></li>
</ul>
</li>
</ul>

This doesen't look like your output to me.

>There's more non-relevant code that I've stripped for a bit of clarity, 
>such as CSS etc. In fact, the actual HTML code is largely irrelavant. 
>Anyway, a barebone version of the perl procedure generating the above is:
>
>#!/usr/bin/perl -w
>
>use Tie::IxHash;
>use strict;
>use warnings;
>
>my $object1 = tie my %listoflinks, "Tie::IxHash";
>
>%listoflinks = ('subject_1.0.html', => 'Subject 1.0',
>                'page1.1.html', => 'Page 1.1',
>                'page1.2.html', => 'Page 1.2',
>                'page1.3.html', => 'Page 1.3');
>
>for (\%listoflinks) {
>my $firstkey = each %$_;
>
>print "<ul>\n"; # open 1st UL
>
>print "<li><a href=$firstkey>$listoflinks{$firstkey}</a>\n"; # open 1st LI
>
>print "<ul>\n"; # open nested UL
>
>while ( local $_ = each %$_ ) { 
>   { print "<li><a href=$_>$listoflinks{$_}</a></li> \n" } # print some LI's
>   }
>
>print "</ul>\n"; # close nested UL
>print "</li>\n"; # close first LI
>print "</ul>\n"; # close first UL
>
>}
>
>The above procedure was put together with good help from this group ages 
>ago.

This looks to be fairly junky code. There is nothing but a linear
generation of html. It might as well have been put together with a
here doc statement.

>As mentioned, the code takes care of the 2-level list structure and 
>does so by fetching the $firstkey from the array entries or LoH and 
>inserting the needed opening and closing UL's and LI's in the right places.
>
>However, I'm not quite sure how to change the script to generate a third 
>level, such as:
>

You havent even been to the second generation, so far its 
really been 1 dimensional.

><ul>
>   <li><a href=subject_1.0.html>Subject 1.0</a>
>      <ul>
>         <li><a href=page_1.1.html>Page 1.1</a></li> 
>         <li><a href=page_1.2.html>Page 1.2</a></li> 
>         <li><a href=page_1.3.html>Page 1.3</a></li> 
>         <li><a href=subject_2.0.html>Subject 2.0</a>
>            <ul>
>               <li><a href=page_2.1.html>Page 2.1</a></li>
>               <li><a href=page_2.1.html>Page 2.2</a></li>
>            </ul>
>         </li>
>     </ul>
>   </li>
></ul>
>
>Or for example, the same structure, with another two second-level list 
>items at the end:
>
><ul>
>   <li><a href=subject_1.0.html>Subject 1.0</a>
>      <ul>
>         <li><a href=page_1.1.html>Page 1.1</a></li> 
>         <li><a href=page_1.2.html>Page 1.2</a></li> 
>         <li><a href=page_1.3.html>Page 1.3</a></li> 
>         <li><a href=subject_2.0.html>Subject 2.0</a>
>            <ul>
>               <li><a href=page_2.1.html>Page 2.1</a></li>
>               <li><a href=page_2.1.html>Page 2.2</a></li>
>            </ul>
>         </li>
>         <li><a href=page_1.4.html>Page 1.4</a></li> 
>         <li><a href=page_1.5.html>Page 1.5</a></li>
>     </ul>
>   </li>
></ul>
>
>Naturally a different array structure would be required in my %listoflinks 
>to output the above. Any advise or examples how this may be pieced together 
>would be most helpful.
>
>Perhaps someone has a procedure in use that does something similar already?

If you want to put together a dynamic html generator, you
will need to write a mini engine to do that.

-sln


------------------------------

Date: Sun, 22 Aug 2010 19:06:14 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Multi-level list generation
Message-Id: <i4rle6$2k0$02$1@news.t-online.com>

sln@netherlands.com wrote:

> On Sat, 21 Aug 2010 21:04:28 +0200, Tuxedo <tuxedo@mailinator.com> wrote:
> 
> >I have a question about how to generate a multi-level (nested) list
> >structure by perl. I currently have a 2-level
> ><ul><li><ul><li></li></ul></li></ul> structure produced via a perl
> >script, which works fine for its purpose. An example HTML output by the
> >existing script is:
> >
> ><ul>
> >   <li><a href=subject_1.0.html>Subject 1.0</a>
> >      <ul>
> >         <li><a href=page_1.1.html>Page 1.1</a></li>
> >         <li><a href=page_1.2.html>Page 1.2</a></li>
> >         <li><a href=page_1.3.html>Page 1.3</a></li>
> >     </ul>
> >   </li>
> ></ul>
> >
> 
> I don't have Tie::IxHash and it is not part of the core.
> After commenting it out and running the code, it prints:
> 
> <ul>
> <li><a href=page1.3.html>Page 1.3</a>
> <ul>
> <li><a href=subject_1.0.html>Subject 1.0</a></li>
> <li><a href=page1.2.html>Page 1.2</a></li>
> <li><a href=page1.1.html>Page 1.1</a></li>
> </ul>
> </li>
> </ul>
> 
> This doesen't look like your output to me.

Yes, because Tie::IxHash puts things in order. Without, %listoflinks will 
appear in a seemingly random order.

> 
> >There's more non-relevant code that I've stripped for a bit of clarity,
> >such as CSS etc. In fact, the actual HTML code is largely irrelavant.
> >Anyway, a barebone version of the perl procedure generating the above is:
> >
> >#!/usr/bin/perl -w
> >
> >use Tie::IxHash;
> >use strict;
> >use warnings;
> >
> >my $object1 = tie my %listoflinks, "Tie::IxHash";
> >
> >%listoflinks = ('subject_1.0.html', => 'Subject 1.0',
> >                'page1.1.html', => 'Page 1.1',
> >                'page1.2.html', => 'Page 1.2',
> >                'page1.3.html', => 'Page 1.3');
> >
> >for (\%listoflinks) {
> >my $firstkey = each %$_;
> >
> >print "<ul>\n"; # open 1st UL
> >
> >print "<li><a href=$firstkey>$listoflinks{$firstkey}</a>\n"; # open 1st
> >LI
> >
> >print "<ul>\n"; # open nested UL
> >
> >while ( local $_ = each %$_ ) {
> >   { print "<li><a href=$_>$listoflinks{$_}</a></li> \n" } # print some
> >   LI's }
> >
> >print "</ul>\n"; # close nested UL
> >print "</li>\n"; # close first LI
> >print "</ul>\n"; # close first UL
> >
> >}
> >
> >The above procedure was put together with good help from this group ages
> >ago.
> 
> This looks to be fairly junky code. There is nothing but a linear
> generation of html. It might as well have been put together with a
> here doc statement.

It fulfilled the purpose it was meant to serve, call it junk or not.

> >As mentioned, the code takes care of the 2-level list structure and
> >does so by fetching the $firstkey from the array entries or LoH and
> >inserting the needed opening and closing UL's and LI's in the right
> >places.
> >
> >However, I'm not quite sure how to change the script to generate a third
> >level, such as:
> >
> 
> You havent even been to the second generation, so far its
> really been 1 dimensional.

Counting the ground floor so to speak, the structure produced by the 
existing script was the following:

<ul>
   <li>first
       <ul>
           <li>second</li>
       </ul>
   </li>
</ul>

Instead what I need is:

<ul>
  <li>first
     <ul>
        <li>second
            <ul>
               <li>third</li>
            </ul>
        </li>
     </ul>
  </li>
</ul>

The current script is not capable of two floors, partly because of the data 
structure and the fact that it simply fetches the firstkey to insert the 
relevant HTML code in the right places.

> 
> ><ul>
> >   <li><a href=subject_1.0.html>Subject 1.0</a>
> >      <ul>
> >         <li><a href=page_1.1.html>Page 1.1</a></li>
> >         <li><a href=page_1.2.html>Page 1.2</a></li>
> >         <li><a href=page_1.3.html>Page 1.3</a></li>
> >         <li><a href=subject_2.0.html>Subject 2.0</a>
> >            <ul>
> >               <li><a href=page_2.1.html>Page 2.1</a></li>
> >               <li><a href=page_2.1.html>Page 2.2</a></li>
> >            </ul>
> >         </li>
> >     </ul>
> >   </li>
> ></ul>
> >
> >Or for example, the same structure, with another two second-level list
> >items at the end:
> >
> ><ul>
> >   <li><a href=subject_1.0.html>Subject 1.0</a>
> >      <ul>
> >         <li><a href=page_1.1.html>Page 1.1</a></li>
> >         <li><a href=page_1.2.html>Page 1.2</a></li>
> >         <li><a href=page_1.3.html>Page 1.3</a></li>
> >         <li><a href=subject_2.0.html>Subject 2.0</a>
> >            <ul>
> >               <li><a href=page_2.1.html>Page 2.1</a></li>
> >               <li><a href=page_2.1.html>Page 2.2</a></li>
> >            </ul>
> >         </li>
> >         <li><a href=page_1.4.html>Page 1.4</a></li>
> >         <li><a href=page_1.5.html>Page 1.5</a></li>
> >     </ul>
> >   </li>
> ></ul>
> >
> >Naturally a different array structure would be required in my
> >%listoflinks to output the above. Any advise or examples how this may be
> >pieced together would be most helpful.
> >
> >Perhaps someone has a procedure in use that does something similar
> >already?
> 
> If you want to put together a dynamic html generator, you
> will need to write a mini engine to do that.

Yes, it sounds like a need a nested list engine of the best make :-)

Tuxedo

> 
> -sln



------------------------------

Date: Sun, 22 Aug 2010 10:21:40 -0500
From: Steve <stevem_@nogood.com>
Subject: Re: Multi-level list generation
Message-Id: <a2dco.2851$cE1.2047@newsfe18.iad>

On 08/22/2010 07:19 AM, Tuxedo wrote:
> Steve wrote:
>
>> On 08/21/2010 03:32 PM, Tuxedo wrote:
>>> Steve wrote:
>>>
>>>> On 08/21/2010 02:04 PM, Tuxedo wrote:
>>>>> I have a question about how to generate a multi-level (nested) list
>>>>> structure by perl. I currently have a 2-level
>>>>> <ul><li><ul><li></li></ul></li></ul>    structure produced via a perl
>>>>> script, which works fine for its purpose. An example HTML output by
>>>>> the existing script is:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> There's more non-relevant code that I've stripped for a bit of
>>>>> clarity, such as CSS etc. In fact, the actual HTML code is largely
>>>>> irrelavant. Anyway, a barebone version of the perl procedure
>>>>> generating the above is:
>>>>>
>>>>> #!/usr/bin/perl -w
>>>>>
>>>>> use Tie::IxHash;
>>>>> use strict;
>>>>> use warnings;
>>>>>
>>>>> my $object1 = tie my %listoflinks, "Tie::IxHash";
>>>>>
>>>>> %listoflinks = ('subject_1.0.html', =>    'Subject 1.0',
>>>>>                    'page1.1.html', =>    'Page 1.1',
>>>>>                    'page1.2.html', =>    'Page 1.2',
>>>>>                    'page1.3.html', =>    'Page 1.3');
>>>>>
>>>>> for (\%listoflinks) {
>>>>> my $firstkey = each %$_;
>>>>>
>>>>> print "<ul>\n"; # open 1st UL
>>>>>
>>>>> print "<li><a href=$firstkey>$listoflinks{$firstkey}</a>\n"; # open
>>>>> 1st LI
>>>>>
>>>>> print "<ul>\n"; # open nested UL
>>>>>
>>>>> while ( local $_ = each %$_ ) {
>>>>>       { print "<li><a href=$_>$listoflinks{$_}</a></li>    \n" } # print
>>>>>       some LI's }
>>>>>
>>>>> print "</ul>\n"; # close nested UL
>>>>> print "</li>\n"; # close first LI
>>>>> print "</ul>\n"; # close first UL
>>>>>
>>>>> }
>>>>>
>>>>> The above procedure was put together with good help from this group
>>>>> ages ago. As mentioned, the code takes care of the 2-level list
>>>>> structure and does so by fetching the $firstkey from the array entries
>>>>> or LoH and inserting the needed opening and closing UL's and LI's in
>>>>> the right places.
>>>>>
>>>>> However, I'm not quite sure how to change the script to generate a
>>>>> third level, such as:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>             <li><a href=subject_2.0.html>Subject 2.0</a>
>>>>>                <ul>
>>>>>                   <li><a href=page_2.1.html>Page 2.1</a></li>
>>>>>                   <li><a href=page_2.1.html>Page 2.2</a></li>
>>>>>                </ul>
>>>>>             </li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> Or for example, the same structure, with another two second-level list
>>>>> items at the end:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>             <li><a href=subject_2.0.html>Subject 2.0</a>
>>>>>                <ul>
>>>>>                   <li><a href=page_2.1.html>Page 2.1</a></li>
>>>>>                   <li><a href=page_2.1.html>Page 2.2</a></li>
>>>>>                </ul>
>>>>>             </li>
>>>>>             <li><a href=page_1.4.html>Page 1.4</a></li>
>>>>>             <li><a href=page_1.5.html>Page 1.5</a></li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> Naturally a different array structure would be required in my
>>>>> %listoflinks to output the above. Any advise or examples how this may
>>>>> be pieced together would be most helpful.
>>>>>
>>>>> Perhaps someone has a procedure in use that does something similar
>>>>> already?
>>>>>
>>>>> Many thanks,
>>>>> Tuxedo
>>>>>
>>>>> NB: System load efficiency is not an issue, as the procedure will run
>>>>> only occasionally on a local machine to generate HTML sent onto a web
>>>>> server in static format. In other words, the script will not run
>>>>> against any real web page requests. The procedure is simply meant to
>>>>> be an easy maintenance tool.
>>>>>
>>>>>
>>>>
>>>> I use recursive routines a *lot* in printing out nested data
>>>> structures, and they are your friend in cases like this...
>>>>
>>>> The below is:
>>>> 1) not tested in any way,
>>>> 2) may not even compile,
>>>> 3) and is just a concept.
>>>>
>>>> %hash = ( whatever, too lazy to make one );
>>>>
>>>> recurse_hash( \%hash );
>>>>
>>>> sub recurse_hash {
>>>>        my $refhash = shift;
>>>>        $refhash or return '';
>>>>
>>>>        print "<ul>\n";
>>>>
>>>>        while( keys %{$refhash} ){
>>>>            if( ref $refhash->{$_} eq 'HASH' ){
>>>>                recurse_hash( $refhash->{$_} );
>>>>            }
>>>>            else{
>>>>                print "<li>$refhash->{$_}</li>\n";
>>>>            }
>>>>        }
>>>>
>>>>        print "</ul>\n";
>>>> }
>>>>
>>>> The beauty of a recursive is it flat doesn't matter how many levels
>>>> deep the data structure is.
>>>>
>>>> The downside is it flat doesn't matter how many levels deep the
>>>> recursive 'thinks' the data structure is and sloppy programming can
>>>> bite you big time..... infinite recursion anyone?
>>>>
>>>> hth,
>>>>
>>>> \s
>>>>
>>>>
>>>
>>> Thanks for the above solution! However, it is a bit difficult for me to
>>> figure exactly how the %hash = (part may be composed) to generate a
>>> multi-level list structure. Any additional pointers anyone?
>>>
>>> Tuxedo
>>>
>>>
>>>
>>
>> Well.....
>>
>> my %hash = (
>>       key1 =>  { subkey =>  'value',
>>                 subhash =>  {
>>                             subsubkey1 =>  'value',
>>                             subsubkey2 =>  'another value',
>>                            },
>>               },
>>       key2 =>  'value',
>>       etc  =>  {
>>               },
>>
>> );
>>
>> But, 'more better' would be to go a reliable (and vetted) source like:
>>
>> http://perldoc.perl.org/perldsc.html
>>
>> hth,
>>
>> \s
>
>
> Thanks for the hash example and perldoc resource. However, it's still a bit
> confusing. To gain a bit further understanding I tried to run your script
> 'as is' but it failed to compile, as of course you warned me it may do. In
> saving your script as a file named testrun.pl and running it, the following
> errors are repeatedly returned to the shell until I hit Ctrl+C:
>
> <li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl
> line 31.
> <li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl
> line 31.
> <li></li>
>
> etc.
>
> Unfortunately I do not understand the meaning of the above errors and if I
> redirect the output to a file, like in ./testrun.pl>  file.txt, the file
> contains a long list of empty<li></li>  containers after an opening<ul>:
>
> <ul>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
>
> etc...
>
> Below is the exact copy of what I tried to run:
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> my %hash = (
>       key1 =>  { subkey =>  'value',
>                 subhash =>  {
>                             subsubkey1 =>  'value',
>                             subsubkey2 =>  'another value',
>                            },
>               },
>       key2 =>  'value',
>
> );
>
>
> recurse_hash( \%hash );
>
> sub recurse_hash {
>       my $refhash = shift;
>       $refhash or return '';
>
>       print "<ul>\n";
>
>       while( keys %{$refhash} ){
>           if( ref $refhash->{$_} eq 'HASH' ){
>               recurse_hash( $refhash->{$_} );
>           }
>           else{
>               print "<li>$refhash->{$_}</li>\n";
>           }
>       }
>
>       print "</ul>\n";
> }
>
> Anyone knows where the error(s) in the above procedure are?
>
> Thanks again,
> Tuxedo
>
>
>

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


------------------------------

Date: Sun, 22 Aug 2010 10:19:39 -0500
From: Steve <stevem_@nogood.com>
Subject: Re: Multi-level list generation
Message-Id: <j0dco.741$V87.575@newsfe04.iad>

On 08/22/2010 07:19 AM, Tuxedo wrote:
> Steve wrote:
>
>> On 08/21/2010 03:32 PM, Tuxedo wrote:
>>> Steve wrote:
>>>
>>>> On 08/21/2010 02:04 PM, Tuxedo wrote:
>>>>> I have a question about how to generate a multi-level (nested) list
>>>>> structure by perl. I currently have a 2-level
>>>>> <ul><li><ul><li></li></ul></li></ul>    structure produced via a perl
>>>>> script, which works fine for its purpose. An example HTML output by
>>>>> the existing script is:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> There's more non-relevant code that I've stripped for a bit of
>>>>> clarity, such as CSS etc. In fact, the actual HTML code is largely
>>>>> irrelavant. Anyway, a barebone version of the perl procedure
>>>>> generating the above is:
>>>>>
>>>>> #!/usr/bin/perl -w
>>>>>
>>>>> use Tie::IxHash;
>>>>> use strict;
>>>>> use warnings;
>>>>>
>>>>> my $object1 = tie my %listoflinks, "Tie::IxHash";
>>>>>
>>>>> %listoflinks = ('subject_1.0.html', =>    'Subject 1.0',
>>>>>                    'page1.1.html', =>    'Page 1.1',
>>>>>                    'page1.2.html', =>    'Page 1.2',
>>>>>                    'page1.3.html', =>    'Page 1.3');
>>>>>
>>>>> for (\%listoflinks) {
>>>>> my $firstkey = each %$_;
>>>>>
>>>>> print "<ul>\n"; # open 1st UL
>>>>>
>>>>> print "<li><a href=$firstkey>$listoflinks{$firstkey}</a>\n"; # open
>>>>> 1st LI
>>>>>
>>>>> print "<ul>\n"; # open nested UL
>>>>>
>>>>> while ( local $_ = each %$_ ) {
>>>>>       { print "<li><a href=$_>$listoflinks{$_}</a></li>    \n" } # print
>>>>>       some LI's }
>>>>>
>>>>> print "</ul>\n"; # close nested UL
>>>>> print "</li>\n"; # close first LI
>>>>> print "</ul>\n"; # close first UL
>>>>>
>>>>> }
>>>>>
>>>>> The above procedure was put together with good help from this group
>>>>> ages ago. As mentioned, the code takes care of the 2-level list
>>>>> structure and does so by fetching the $firstkey from the array entries
>>>>> or LoH and inserting the needed opening and closing UL's and LI's in
>>>>> the right places.
>>>>>
>>>>> However, I'm not quite sure how to change the script to generate a
>>>>> third level, such as:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>             <li><a href=subject_2.0.html>Subject 2.0</a>
>>>>>                <ul>
>>>>>                   <li><a href=page_2.1.html>Page 2.1</a></li>
>>>>>                   <li><a href=page_2.1.html>Page 2.2</a></li>
>>>>>                </ul>
>>>>>             </li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> Or for example, the same structure, with another two second-level list
>>>>> items at the end:
>>>>>
>>>>> <ul>
>>>>>       <li><a href=subject_1.0.html>Subject 1.0</a>
>>>>>          <ul>
>>>>>             <li><a href=page_1.1.html>Page 1.1</a></li>
>>>>>             <li><a href=page_1.2.html>Page 1.2</a></li>
>>>>>             <li><a href=page_1.3.html>Page 1.3</a></li>
>>>>>             <li><a href=subject_2.0.html>Subject 2.0</a>
>>>>>                <ul>
>>>>>                   <li><a href=page_2.1.html>Page 2.1</a></li>
>>>>>                   <li><a href=page_2.1.html>Page 2.2</a></li>
>>>>>                </ul>
>>>>>             </li>
>>>>>             <li><a href=page_1.4.html>Page 1.4</a></li>
>>>>>             <li><a href=page_1.5.html>Page 1.5</a></li>
>>>>>         </ul>
>>>>>       </li>
>>>>> </ul>
>>>>>
>>>>> Naturally a different array structure would be required in my
>>>>> %listoflinks to output the above. Any advise or examples how this may
>>>>> be pieced together would be most helpful.
>>>>>
>>>>> Perhaps someone has a procedure in use that does something similar
>>>>> already?
>>>>>
>>>>> Many thanks,
>>>>> Tuxedo
>>>>>
>>>>> NB: System load efficiency is not an issue, as the procedure will run
>>>>> only occasionally on a local machine to generate HTML sent onto a web
>>>>> server in static format. In other words, the script will not run
>>>>> against any real web page requests. The procedure is simply meant to
>>>>> be an easy maintenance tool.
>>>>>
>>>>>
>>>>
>>>> I use recursive routines a *lot* in printing out nested data
>>>> structures, and they are your friend in cases like this...
>>>>
>>>> The below is:
>>>> 1) not tested in any way,
>>>> 2) may not even compile,
>>>> 3) and is just a concept.
>>>>
>>>> %hash = ( whatever, too lazy to make one );
>>>>
>>>> recurse_hash( \%hash );
>>>>
>>>> sub recurse_hash {
>>>>        my $refhash = shift;
>>>>        $refhash or return '';
>>>>
>>>>        print "<ul>\n";
>>>>
>>>>        while( keys %{$refhash} ){
>>>>            if( ref $refhash->{$_} eq 'HASH' ){
>>>>                recurse_hash( $refhash->{$_} );
>>>>            }
>>>>            else{
>>>>                print "<li>$refhash->{$_}</li>\n";
>>>>            }
>>>>        }
>>>>
>>>>        print "</ul>\n";
>>>> }
>>>>
>>>> The beauty of a recursive is it flat doesn't matter how many levels
>>>> deep the data structure is.
>>>>
>>>> The downside is it flat doesn't matter how many levels deep the
>>>> recursive 'thinks' the data structure is and sloppy programming can
>>>> bite you big time..... infinite recursion anyone?
>>>>
>>>> hth,
>>>>
>>>> \s
>>>>
>>>>
>>>
>>> Thanks for the above solution! However, it is a bit difficult for me to
>>> figure exactly how the %hash = (part may be composed) to generate a
>>> multi-level list structure. Any additional pointers anyone?
>>>
>>> Tuxedo
>>>
>>>
>>>
>>
>> Well.....
>>
>> my %hash = (
>>       key1 =>  { subkey =>  'value',
>>                 subhash =>  {
>>                             subsubkey1 =>  'value',
>>                             subsubkey2 =>  'another value',
>>                            },
>>               },
>>       key2 =>  'value',
>>       etc  =>  {
>>               },
>>
>> );
>>
>> But, 'more better' would be to go a reliable (and vetted) source like:
>>
>> http://perldoc.perl.org/perldsc.html
>>
>> hth,
>>
>> \s
>
>
> Thanks for the hash example and perldoc resource. However, it's still a bit
> confusing. To gain a bit further understanding I tried to run your script
> 'as is' but it failed to compile, as of course you warned me it may do. In
> saving your script as a file named testrun.pl and running it, the following
> errors are repeatedly returned to the shell until I hit Ctrl+C:
>
> <li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl
> line 31.
> <li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl
> line 31.
> <li></li>
>
> etc.
>
> Unfortunately I do not understand the meaning of the above errors and if I
> redirect the output to a file, like in ./testrun.pl>  file.txt, the file
> contains a long list of empty<li></li>  containers after an opening<ul>:
>
> <ul>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
> <li></li>
>
> etc...
>
> Below is the exact copy of what I tried to run:
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> my %hash = (
>       key1 =>  { subkey =>  'value',
>                 subhash =>  {
>                             subsubkey1 =>  'value',
>                             subsubkey2 =>  'another value',
>                            },
>               },
>       key2 =>  'value',
>
> );
>
>
> recurse_hash( \%hash );
>
> sub recurse_hash {
>       my $refhash = shift;
>       $refhash or return '';
>
>       print "<ul>\n";
>
>       while( keys %{$refhash} ){
>           if( ref $refhash->{$_} eq 'HASH' ){
>               recurse_hash( $refhash->{$_} );
>           }
>           else{
>               print "<li>$refhash->{$_}</li>\n";
>           }
>       }
>
>       print "</ul>\n";
> }
>
> Anyone knows where the error(s) in the above procedure are?
>
> Thanks again,
> Tuxedo
>
>
>

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


------------------------------

Date: Sun, 22 Aug 2010 10:36:33 -0500
From: Steve <stevem_@nogood.com>
Subject: Re: Multi-level list generation
Message-Id: <8gdco.16423$wJ1.4823@newsfe08.iad>

On 08/22/2010 10:17 AM, Tuxedo wrote:
> Uri Guttman wrote:
>
>>>>>>> "c" == ccc31807<cartercc@gmail.com>  writes:
>>
>>    c>  On Aug 21, 3:04 pm, Tuxedo<tux...@mailinator.com>  wrote:
>>    >>  I have a question about how to generate a multi-level (nested) list
>>    >>  structure by perl. I currently have a 2-level
>>    >>  <ul><li><ul><li></li></ul></li></ul>  structure produced via a perl
>>    >>  script, which works fine for its purpose. An example HTML output by
>>    >>  the existing script is:
>>
>>    c>  Mark Jason Dominus, in 'Higher Order Perl' has a section on parsing
>>    c>  HTML in chapter 1. You can download the book over the internet, but
>>    c>  it's well worth buying, and I would encourage you to do so.
>>
>> the OP isn't parsing but generating html. parsing it should be done with
>> a module. generating it is done well with templates but he still needs
>> to learn data structures to work with them. regardless of the
>> technology, nested html needs nested data which means perl data
>> structures. they are used in some many perl programs that they are
>> critical to learn early one. perlreftut, perldsc and perllol are
>> required reading from the perl docs.
>>
>> uri
>>
>
> Will look into perlreftut, perldsc and perllol. My task may seem trivial,
> but as you say, the relevant data structures are needed, which makes this
> one a hard nut to crack without a fairly deep level of perl knowledge.
>
> Tuxedo


Uri is right in his response that you *must* learn to deal with complex 
data structures.

I've been using Perl to generate web pages since the mid '90's and I can 
promise you that if you take the time to learn how to deal with nested 
data, how to access it, and how to create it, you will be amazed at how 
much simpler your job becomes.

So, suck it up and go for it. It will only hurt for a little while. :-)

\s








------------------------------

Date: Sun, 22 Aug 2010 12:49:53 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Multi-level list generation
Message-Id: <slrni72oi7.e8c.tadmc@tadbox.sbcglobal.net>

Tuxedo <tuxedo@mailinator.com> wrote:

> 'as is' but it failed to compile, 


None of the messages you've shown indicate a failure to compile.

What makes you think compilation failed?


> as of course you warned me it may do. In 
> saving your script as a file named testrun.pl and running it, the following 
> errors are repeatedly returned to the shell until I hit Ctrl+C:
>
><li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl 
> line 31.
><li></li>
> Use of uninitialized value in hash element at ./testrun.pl line 27.
> Use of uninitialized value in hash element at ./testrun.pl line 31.
> Use of uninitialized value in concatenation (.) or string at ./testrun.pl 
> line 31.


None of those are error messages.

They are warning messages.


> Unfortunately I do not understand the meaning of the above errors 


Even after looking up the messages in Perl's standard docs?

    perldoc perldiag


> and if I 
> redirect the output to a file, like in ./testrun.pl > file.txt, the file 
> contains a long list of empty <li></li> containers after an opening <ul>:


If the program ran, then it must have compiled successfully...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Sun, 22 Aug 2010 09:42:57 -0700 (PDT)
From: king <hara.acharya@gmail.com>
Subject: Re: regular expression for beow text
Message-Id: <83289b9c-e3f9-4b5d-976d-5d2d114ad4fb@k1g2000prl.googlegroups.com>

On Aug 21, 8:47=A0pm, s...@netherlands.com wrote:
> On Sat, 21 Aug 2010 04:52:39 -0700 (PDT), king <hara.acha...@gmail.com> w=
rote:
> >Hi,
> >Well what I am trying to do is somehting as below:
> >My script:
> >#!/c/perl/bin
> >open(SYSCMD, "dir /A:A /O:D |");
> >while(<SYSCMD>)
> >{
> > =A0 =A0#if(/^(\s+)(\d)\s/)
> > =A0 =A0#if(/^(\s+)(\d+)\s/)
> > =A0 =A0if(/^[\d\/]+\s+[\d:]+\s+[APM]+\s+[\d,]+\s+([^\n]+)/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0$count =3D $_;
> > =A0 =A0}
> >}
> >close(SYSCMD);
> >print "$count\n";
>
> I think you would get better results if you include all
> files (and directories), the /A:A is going to exclude some files.
> =A0:\> dir /O:D is all thats needed, the latest is at the bottom.
>
> -sln
> ----------------------
>
> use strict;
Thanks a lot for this..this solution works perfectly.

Regards

> use warnings;
>
> my ($cnt, $last_file) =3D (0, '');
> open(SYSCMD, "dir /O:D |") or die "open pipe failed: $!";
>
> while (<SYSCMD>) {
> =A0 =A0 if (/^[\d\/]+ \s+ [\d:]+ \s+ [APM]+ \s+ [\d,]+ \s+ ([^\n]+) /x) {
> =A0 =A0 =A0 =A0 $cnt++;
> =A0 =A0 =A0 =A0 $last_file =3D $1;
> =A0 =A0 }}
>
> close(SYSCMD);
>
> print "$cnt =A0 '$last_file'";



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3093
***************************************


home help back first fref pref prev next nref lref last post