Perl flattens lists, doc or explanation wanted 
Author Message
 Perl flattens lists, doc or explanation wanted

I understand that perl "combines" lists when calling
a subroutine.

It also seems that Perl "flattens" lists, i.e. prevents
the creation of nested lists unless you directly or
indirectly use references.

I once read a clear explanation of this.  Any pointers would be
appreciated.  It is not in the perltrap, perlfaq, or perltut that I
have installed.  I recall this is a difference between Perl and
Python, and maybe that's where I read it.

Hopefully helpfully yours,
Steve
--

Fidelity Investments   82 Devonshire St. R24D    Boston MA 02109
There is nothing so practical as a good theory.  Comments are by me,
not Fidelity Investments, its subsidiaries or affiliates.



Tue, 26 Aug 2003 00:58:10 GMT  
 Perl flattens lists, doc or explanation wanted

Quote:

> I understand that perl "combines" lists when calling
> a subroutine.
> It also seems that Perl "flattens" lists, i.e. prevents
> the creation of nested lists unless you directly or
> indirectly use references.
> I once read a clear explanation of this.  Any pointers would be
> appreciated.  It is not in the perltrap, perlfaq, or perltut that I
> have installed.  I recall this is a difference between Perl and
> Python, and maybe that's where I read it.

Good to read the docs. You just missed finding the right one.
perldoc perlsub

Chris

--
Christopher E. Stith
You must not lose faith in humanity. Humanity is an ocean;
if a few drops of the ocean are dirty, the ocean does not
become dirty.  -- Mohandas K. Gandhi



Wed, 27 Aug 2003 04:24:31 GMT  
 Perl flattens lists, doc or explanation wanted


: I understand that perl "combines" lists when calling
: a subroutine.
:
: It also seems that Perl "flattens" lists, i.e. prevents
: the creation of nested lists unless you directly or
: indirectly use references.
:
: I once read a clear explanation of this.  Any pointers would be
: appreciated.  It is not in the perltrap, perlfaq, or perltut that I
: have installed.  I recall this is a difference between Perl and
: Python, and maybe that's where I read it.

The persub manpage contains

    The Perl model for function call and return values is simple: all
    functions are passed as parameters one single flat list of scalars,
    and all functions likewise return to their caller one single flat
    list of scalars.  Any arrays or hashes in these call and return
    lists will collapse, losing their identities--but you may always
    use pass-by- reference instead to avoid this.  Both call and return
    lists may contain as many or as few scalar elements as you'd like.

    [...]

    If you return one or more aggregates (arrays and hashes), these will
    be flattened together into one large indistinguishable list.

Hope this helps,
Greg
--
The most exciting phrase to hear in science, the one that heralds new
discoveries, is not "Eureka!" (I found it!) but "That's funny..."
    -- Isaac Asimov



Tue, 26 Aug 2003 23:35:53 GMT  
 Perl flattens lists, doc or explanation wanted

Quote:

> I once read a clear explanation of this.  Any pointers would be
> appreciated.  It is not in the perltrap, perlfaq, or perltut that I
> have installed.  I recall this is a difference between Perl and
> Python, and maybe that's where I read it.

Perhaps you're thinking of "perllol". Try "perlref" and "perldsc" as well.

-- Dave Tweed



Tue, 26 Aug 2003 10:29:39 GMT  
 Perl flattens lists, doc or explanation wanted

steve> I understand that perl "combines" lists when calling
steve> a subroutine.

steve> It also seems that Perl "flattens" lists, i.e. prevents
steve> the creation of nested lists unless you directly or
steve> indirectly use references.

steve> I once read a clear explanation of this.  Any pointers would be
steve> appreciated.  It is not in the perltrap, perlfaq, or perltut that I
steve> have installed.  I recall this is a difference between Perl and
steve> Python, and maybe that's where I read it.

This:


is the same as this:


which is the same as this:



In other words, there are no lists of lists in Perl.  They're always
"flat".  And as you said, you can get to what most people call
"lists of lists" by using lists containing some arrayrefs:

        $arrayref = [2, 3, 4];


LISP-y terms, it'd be (1 (2 3 4) 5).  But the dereferencing is always
explicit.  A list contains only scalars, but some of those scalars can
be pointers to other arrays (lists).

Does that help?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 27 Aug 2003 01:08:33 GMT  
 Perl flattens lists, doc or explanation wanted

Quote:
> This:


> is the same as this:


> which is the same as this:




Which is, of course, because the parentheses do not *create* the
lists, they just group the lower-precedence commas apart from the
higher-precedence assignment.  Interspersing extra precedence
parentheses, which is all these are, is simply redundant.  Well,
unless it isn't... :)

--
Ren Maddox



Sat, 30 Aug 2003 07:03:15 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. MEMO with SQL

2. explanation wanted ...

3. perl script for mailing list wanted

4. Wanted: a list of really useful perl scripts.

5. perl for mailing list management wanted

6. WANTED: A PERL script to verify an ASCII list of URLs for 404 errors

7. dbase vs paradox

8. Pause/break

9. text search ib blobs

10. DB applications: VBasic and Delphi

11. API interfacing C++ derived classes without flattening base methods

12. 'Flatten' array of phrases

 

 
Powered by phpBB® Forum Software