[31858] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3121 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 7 00:09:25 2010

Date: Mon, 6 Sep 2010 21:09:06 -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           Mon, 6 Sep 2010     Volume: 11 Number: 3121

Today's topics:
    Re: Accessing and printing LoL fields in a loop? <tadmc@seesig.invalid>
    Re: Ideal data structure for nested list format? <ben@morrow.me.uk>
    Re: Ideal data structure for nested list format? <tuxedo@mailinator.com>
    Re: Ideal data structure for nested list format? <uri@StemSystems.com>
    Re: Ideal data structure for nested list format? <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 06 Sep 2010 19:57:30 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Accessing and printing LoL fields in a loop?
Message-Id: <slrni8b30u.ijh.tadmc@tadbox.sbcglobal.net>

Tuxedo <tuxedo@mailinator.com> wrote:

> As far as I understand, a nested list system is created based on the 
> directory depth levels of your site.


I cannot resist the chance to repost this amazing line of code
that I saved from some post long ago (with a small mod suggested
by Ilya):

   find . -print | sed -e 's,[^/]*/\([^/]*\)$,|`--\1,' -e 's,[^/]*/,|  ,g'


-- 
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: Mon, 6 Sep 2010 21:39:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Ideal data structure for nested list format?
Message-Id: <640gl7-jui1.ln1@osiris.mauzo.dyndns.org>


Quoth Tuxedo <tuxedo@mailinator.com>:
> Ben Morrow wrote:
> 
> > One of these two calls *doesn't* pass an arrayref as the second argument.
> 
> Should it be:
> 
> \@$kids

That derefs $kids (giving an array) and then immediately takes a new ref
to that array. There's no need for that: just pass $kids directly. It's
already an arrayref.

> At least there are no confusing errors now. Can you give any further hints 
> how to print the items in a nested list structure? The script so-far is:
> 
> #!/usr/bin/perl -w
> 
> use warnings;
> use strict;
> 
> my @pages = (
>         { page => "1.1.html", title => "Level 1.1", children => [
>             { page => "2.1.html", title => "Level 2.1" },
>             { page => "2.2.html", title => "Level 2.2", children => [
>                 { page => "3.1.html", title => "Level 3.1" },
>                 { page => "3.2.html", title => "Level 3.2" },
>             ] },
>             { page => "2.3.html", title => "Level 2.3" },
>         ] },
>     );

You need to put this data structure, and %parents and the call to
populate_parents below, in a block. Otherwise @pages is scoped over the
whole file, and you will find yourself picking it up accidentally when
you mistype something (as you did when you changes @$pages to \@pages
before).

> #and then build a hash of 'parents', like so
> 
>     sub populate_parents;
>     sub populate_parents {
>         my ($parents, $pages, $parent) = @_;
> 
>         for (@$pages) {
>             my $page = $_->{page};
>             push @{$parents->{$page}}, $parent;
>             my $kids = $_->{children};
>             $kids and populate_parents $parents, \@$kids, $page;
>         }
>     }
> 
>     my %parents;
>     populate_parents \%parents, \@pages, undef;
> 
> # This will give you a data structure like
> 
>     %parents = (
>         "1.1.html" => [undef],
>         "2.1.html" => [undef, "1.1.html"],
>         "3.1.html" => [undef, "1.1.html", "2.2.html"],
> #        ...
>     );

You don't need this last bit. It replaces the data structure
populate_parents has just built with an incomplete version of the same
thing. It was only intended as an example, so you could understand the
sort of structure populate_parents was building. That was what the '...'
was intended to indicate: sorry if that wasn't clear.

I think you need to take a step back, and go through the whole thing
again with perlreftut and perldsc in your hand until you understand what
it's doing and how it does it. I'm not going to give you any more bits
of sample code until you've understood these bits, because I really
believe that using bits of code you don't understand is dangerous.

Ben



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

Date: Mon, 6 Sep 2010 22:56:44 +0200
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Ideal data structure for nested list format?
Message-Id: <i63kic$qrq$03$1@news.t-online.com>

Ben Morrow wrote:

[...]

> I think you need to take a step back, and go through the whole thing
> again with perlreftut and perldsc in your hand until you understand what
> it's doing and how it does it. I'm not going to give you any more bits
> of sample code until you've understood these bits, because I really
> believe that using bits of code you don't understand is dangerous.

I'm always looking forward to a message from you. Each of your answers is a 
challange for me. I admit, I'm not the brightest student but you can be 
sure I tried my best. Actually, I'm sitting day and night over the code but 
I'm afraid I need another hint. It's not that I don't want to learn, I 
simply can't see the light at the end of the tunnel....

Thanks for your help so far.

Tuxedo


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

Date: Mon, 06 Sep 2010 17:19:38 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Ideal data structure for nested list format?
Message-Id: <877hiyk1sl.fsf@quad.sysarch.com>

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

  T> my @pages = (
  T>         { page => "1.1.html", title => "Level 1.1", children => [
  T>             { page => "2.1.html", title => "Level 2.1" },
  T>             { page => "2.2.html", title => "Level 2.2", children => [
  T>                 { page => "3.1.html", title => "Level 3.1" },
  T>                 { page => "3.2.html", title => "Level 3.2" },
  T>             ] },
  T>             { page => "2.3.html", title => "Level 2.3" },
  T>         ] },
  T>     );

it will help you immensely if you formatted your data better. you have
indents but it still is hard to read IMO

some rules:

don't put data on the same line as the opening brace/bracket.

put each pair on its own line.

tab/indent the => to be the same column

use hard tabs (yes, it is controversial but the hard tab camp has a
better argument). hard tabs can be DISPLAYED to any tab width you want
with all decent code editors. the hard tab count accurately represents
the indent level.

always put commas after every element, regardless if it is the last one
in a list/hash. perl allows this (most other langs don't). it makes it
much easier to add/delete/move elements without worrying about the
trailing or other comma.

this is a general rule, use single quotes when the string doesn't do
interpolation, double quotes when it does. these should all be single
quotes.


my @pages = ( {
	page	=> '1.1.html',
	title	=> 'Level 1.1',
	children => [
		{
			page	=> '2.1.html',
			title	=> 'Level 2.1',
		},
		{
			page	=> '2.2.html',
                        title	=> 'Level 2.2',
			children => [
		                {
					page	=> '3.1.html',
                                        title	=> 'Level 3.1',
				},
		                {
					page	=> '3.2.html',
					title	=> 'Level 3.2',
				},
			],
		},
		{
			page	=> '2.3.html',
			title	=> 'Level 2.3',
		},
	],
}, );


that is much easier to read IMNSHO and will likely help you in coding up
your tree traversal too.

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: Tue, 7 Sep 2010 04:21:26 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Ideal data structure for nested list format?
Message-Id: <mmngl7-6kn1.ln1@osiris.mauzo.dyndns.org>


Quoth Tuxedo <tuxedo@mailinator.com>:
> Ben Morrow wrote:
> 
> > I think you need to take a step back, and go through the whole thing
> > again with perlreftut and perldsc in your hand until you understand what
> > it's doing and how it does it. I'm not going to give you any more bits
> > of sample code until you've understood these bits, because I really
> > believe that using bits of code you don't understand is dangerous.
> 
> I'm always looking forward to a message from you. Each of your answers is a 
> challange for me. I admit, I'm not the brightest student but you can be 
> sure I tried my best. Actually, I'm sitting day and night over the code but 
> I'm afraid I need another hint. It's not that I don't want to learn, I 
> simply can't see the light at the end of the tunnel....

Where are you stuck, specifically, when it comes to understanding the
code you've got? Once you've actually understood what that does you
should be able to write the printing code on your own.

Ben



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

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 3121
***************************************


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