list vs. array

Discussion in 'Perl Misc' started by Robert Suyemoto, Sep 25, 2006.

  1. Hello.

    I have recently heard the claim that in Perl,
    "list" and "array" are two separate things, in that
    one is of fixed length and the other isn't.

    (Such that for the fixed length type, you could not
    use push or pop on it.)

    I have never heard of anything like this, but in
    the interest of keeping an open mind, is there any
    context where this would be true?


    Thanks very much,
    R.
    Robert Suyemoto, Sep 25, 2006
    #1
    1. Advertising

  2. Robert Suyemoto

    David Squire Guest

    Robert Suyemoto wrote:
    > Hello.
    >
    > I have recently heard the claim that in Perl,
    > "list" and "array" are two separate things, in that
    > one is of fixed length and the other isn't.
    >
    > (Such that for the fixed length type, you could not
    > use push or pop on it.)
    >
    > I have never heard of anything like this,


    Gee. You could have tried reading the documentation that comes with
    Perl. When I type "perldoc -q list vs array", the third entry is
    entitled "What is the difference between a list and an array?". The
    first two sentences of that entry are "An array has a changeable length.
    A list does not."

    That is not the only difference. I suggest that you read the documentation.


    DS
    David Squire, Sep 25, 2006
    #2
    1. Advertising

  3. Robert Suyemoto

    Paul Lalli Guest

    Robert Suyemoto wrote:

    > I have recently heard the claim


    you mean the fact.

    > that in Perl,
    > "list" and "array" are two separate things, in that
    > one is of fixed length and the other isn't.


    One is a constant (or literal), one is a variable.

    > (Such that for the fixed length type, you could not
    > use push or pop on it.)
    >
    > I have never heard of anything like this, but in
    > the interest of keeping an open mind, is there any
    > context where this would be true?


    This is a list: ('foo', 'bar', 'baz')
    This is an array: @stuff;

    An array is a variable that contains a list of scalars. It is (mostly)
    analogous to the difference between a scalar value, like 'Hello World',
    and the variable $phrase which might contain it. You can change
    $phrase by concatenating to it or using a s/// operator, but you can't
    change an actual literal string:

    #this works, obviously:
    $string =~ s/Hello/Goodbye/;
    #but this gives "Can't modify constant item in substitution (s///) "
    'Hello World' =~ s/Hello/Goodbye/;

    Similarly,
    #this works
    push @stuff, 'bam';
    #but this gives "Type of arg 1 to push must be array (not constant
    item)"
    push ('foo', 'bar', 'baz'), 'bam';


    See also: perldoc -q difference
    Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
    What is the difference between a list and an array?

    Paul Lalli
    Paul Lalli, Sep 25, 2006
    #3
  4. Robert Suyemoto

    John Bokma Guest

    Robert Suyemoto <> wrote:

    > Hello.
    >
    > I have recently heard the claim that in Perl,
    > "list" and "array" are two separate things,


    perldoc -q list


    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Sep 25, 2006
    #4
  5. Robert Suyemoto

    Uri Guttman Guest

    >>>>> "PL" == Paul Lalli <> writes:

    PL> Robert Suyemoto wrote:
    >> I have recently heard the claim


    PL> you mean the fact.

    >> that in Perl,
    >> "list" and "array" are two separate things, in that
    >> one is of fixed length and the other isn't.


    PL> One is a constant (or literal), one is a variable.

    i would say a list is a value (could be generated from a func call or
    expression) and an array is a variable which can hold a list.

    other major points i use to explain this:

    lists live on the stack and are live only during that expression.
    arrays live on the heap (allocated) and can be alive as long as you want

    list have no way to be referenced later
    arrays can have references and be anonymous and used in data trees

    lists can never have a name
    arrays could have names

    lists are fixed size since they are the result of an expression
    arrays can have their size changed since they are in variables

    lists and arrays can be indexed and sliced. this is about the only
    operation common to both.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 25, 2006
    #5
  6. Robert Suyemoto

    Guest


    > i would say a list is a value (could be generated from a func call or
    > expression) and an array is a variable which can hold a list.
    >


    Oy!

    IMO, what a crazy distinction. Especially since the words 'array' and
    'list' have meanings in the real world - for example, different kinds
    of Abstract Data Types.
    , Sep 25, 2006
    #6
  7. Robert Suyemoto

    David Squire Guest

    wrote:
    >> i would say a list is a value (could be generated from a func call or
    >> expression) and an array is a variable which can hold a list.
    >>

    >
    > Oy!
    >
    > IMO, what a crazy distinction. Especially since the words 'array' and
    > 'list' have meanings in the real world - for example, different kinds
    > of Abstract Data Types.
    >


    Oh for goodness sake! All programming languages give their reserved
    words specific and restricted means compared to the usage of that word
    in the "real world". Get used to it. There is a difference between a
    Perl array and a Field Programmable Gate array too.

    Ah you saying that the distinction between a string variable and a
    string literal in almost every programming language is also "crazy"?

    There is an important distinction between a variable and the values it
    can represent?

    'This is a string literal';
    my $string; # this is a variable capable of representing a string
    (amongst other things).


    DS
    David Squire, Sep 25, 2006
    #7
  8. Robert Suyemoto

    Guest

    David Squire wrote:
    > Oh for goodness sake!


    :)

    >
    > Ah you saying that the distinction between a string variable and a
    > string literal in almost every programming language is also "crazy"?
    >


    Not at all. Especially as you named them, respectively:
    "string variable", and
    "string literal".

    But as soon as you'd call one a "string", and the other something like
    "text block", then it gets suspicious.

    Now applying (your!) naming logic (which I agree with), two names such
    as:
    "list variable", and
    "list literal"...

    would generate a guaranteed 10% reduction in pointless questions to
    c.l.p.m.
    , Sep 25, 2006
    #8
  9. <> wrote:


    [ Please provide an attribution when you quote someone. ]


    >> i would say a list is a value (could be generated from a func call or
    >> expression) and an array is a variable which can hold a list.
    >>

    >
    > Oy!
    >
    > IMO, what a crazy distinction.



    But one that must be understood if you hope to understand Perl.

    It can be crazy, and you can argue it if you like, but you still
    need to make the distinction if you need to make sense of Perl.


    > Especially since the words 'array' and
    > 'list' have meanings in the real world -



    And they have meanings in the Perl world.


    > for example, different kinds
    > of Abstract Data Types.



    for example, data and a variable that can hold that kind of data.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 26, 2006
    #9
  10. Robert Suyemoto

    Uri Guttman Guest

    >>>>> "ro" == robb@acm org <> writes:

    ro> Now applying (your!) naming logic (which I agree with), two names such
    ro> as:
    ro> "list variable", and
    ro> "list literal"...

    ro> would generate a guaranteed 10% reduction in pointless questions to
    ro> c.l.p.m.

    balderdash. people reading the docs would lower the pointless questions
    to almost 0. computer stuff is about being exact in what you code and
    that means exact in the meanings of terms. perl like all other projects
    chose its names and terms and the key is to be consistant with them, not
    to make them cater to those who don't care. if you want to hack perl you
    talk perl. you don't talk cobol or python here. lists and arrays are
    well defined in perl and they are not the same. did you even read the
    differences i listed? did you understand how they are actually more
    different than they are the same? do you understand the concept of a
    value vs a variable (types don't matter)? string variable vs string
    literal. the difference is variable vs literal and string doesn't
    matter. list value and array variable. notice the differences there?
    both terms are different so they both mean something. lists are not
    variables nor arrays. arrays are not values (but they contain a value
    and can supply that value to an expression).

    learn perl and stop asking for perl to learn you.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 26, 2006
    #10
  11. Robert Suyemoto

    Guest

    Uri Guttman wrote:

    > did you even read the differences i listed?


    Of course. No reason to get rude. I'm approaching the language like I
    approach all languages - as a computer scientist. By using academic-
    and industry-standard language, it gives us a common way to
    communicate. I've read all the perldocs and many posts. I haven't
    looked at the language specification, though. Maybe that'd be helpful.

    I couldn't really understand what you're saying in your last post;

    Are you talking about the mutability of the data structure?

    Or, are you talking about the fact that a "list" is immutable simply
    because it's literally specified?

    Are you saying that the list notation causes an immutable data
    structure to be instantiated, whereas array notation causes a mutable
    one to be?

    It doesn't sound like it, since a list can be assiged to a named
    variable, and it's then mutable.

    How would a "list variable" differ from an array?

    How would an anonymous array differ from a list?
    , Sep 26, 2006
    #11
  12. Robert Suyemoto

    Uri Guttman Guest

    >>>>> "ro" == robb@acm org <> writes:

    ro> Uri Guttman wrote:
    >> did you even read the differences i listed?


    ro> Of course. No reason to get rude. I'm approaching the language
    ro> like I approach all languages - as a computer scientist. By using
    ro> academic- and industry-standard language, it gives us a common way
    ro> to communicate. I've read all the perldocs and many posts. I
    ro> haven't looked at the language specification, though. Maybe
    ro> that'd be helpful.

    you were rude in bitching about lists and arrays in your terminology. i
    was responding in kind. there is no language spec for perl5 (perl6 has
    one). and if you really are a computer scientist (and not just playing
    one on tv) you should know that langs use terms in very specific ways and
    they won't bend to your tastes. and also there is no academic and
    industry standard language for any of this. have you seen how many
    different overloadings of common terms are out there?

    ro> I couldn't really understand what you're saying in your last post;

    ro> Are you talking about the mutability of the data structure?

    ro> Or, are you talking about the fact that a "list" is immutable simply
    ro> because it's literally specified?

    ????

    what i said was very clear and any computer scientist should be able to
    understand them.

    ro> Are you saying that the list notation causes an immutable data
    ro> structure to be instantiated, whereas array notation causes a mutable
    ro> one to be?

    there is no list notation. where do you get ideas like that? there is no
    special syntax for lists in perl. none. nada. nil. bupkis. a list is
    made by having a list context and some data that is in that context. no
    syntax anywhere.

    ro> It doesn't sound like it, since a list can be assiged to a named
    ro> variable, and it's then mutable.

    some CS you are. you don't seem to get the difference between a variable
    and a value.

    ro> How would a "list variable" differ from an array?

    who ever said they are different or the same? perl has arrays and
    lists. i was making a point about terminology and what is important in
    naming things. variables are not values. lists are values. simple.

    ro> How would an anonymous array differ from a list?

    as i said, the anon part has nothing to do with a list. anon arrays are
    really array refs. you can't take a ref of a list as the list is on the
    stack and refs refer to heap allocations. so anon and lists are apples
    and oranges.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 26, 2006
    #12
  13. <> wrote:

    > How would an anonymous array differ from a list?



    An array is on the heap, and a list is on the stack. (that sounds familiar...)


    That doesn't help those students who don't have much computer science
    though, so I try to put it into layman's terms:

    The list is gone once you get the semicolon that ends the
    statement it appears in. An array (named or not) can live on.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 26, 2006
    #13
  14. Robert Suyemoto

    Guest

    Tad McClellan wrote:
    >
    > An array is on the heap, and a list is on the stack. (that sounds familiar...)
    >


    Thanks - I hadn't seen it expressed this concisely up till now.

    Is it advisable to think about these issues and consider them when
    actually doing programming?

    For example, when I work with lists and arrays, I usually focus on
    issues such as do I want to be manipulating arrays or arrayrefs...
    Which do I want to be using in my APIs, etc.

    Now that I think about it, I think that part of my confusion is because
    I'm used to programming in languages where all variables are references
    - Python, Java, etc.
    , Sep 26, 2006
    #14
  15. Robert Suyemoto

    Uri Guttman Guest

    >>>>> "ro" == robb@acm org <> writes:

    ro> Tad McClellan wrote:
    >>
    >> An array is on the heap, and a list is on the stack. (that sounds familiar...)
    >>


    ro> Thanks - I hadn't seen it expressed this concisely up till now.

    did you read my post? did you see (that sounds familiar...) there? tad
    was refering to my post which said the exact same thing. bah.

    ro> Is it advisable to think about these issues and consider them when
    ro> actually doing programming?

    ro> For example, when I work with lists and arrays, I usually focus on
    ro> issues such as do I want to be manipulating arrays or arrayrefs...
    ro> Which do I want to be using in my APIs, etc.

    that makes no sense whatsoever.

    ro> Now that I think about it, I think that part of my confusion is because
    ro> I'm used to programming in languages where all variables are references
    ro> - Python, Java, etc.

    that is your problem. you are stuck in other langs world view. this is
    perl. learn perl to code in perl. drop all concepts of what you think
    perl should be like. perl isn't going to change for you. so you have to
    change for perl.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 26, 2006
    #15
  16. Robert Suyemoto

    Keith Keller Guest

    On 2006-09-26, <> wrote:
    >
    > Tad McClellan wrote:
    >>
    >> An array is on the heap, and a list is on the stack. (that sounds familiar...)
    >>

    >
    > Thanks - I hadn't seen it expressed this concisely up till now.


    Uri said it in the exact post to which you posted a followup saying the
    distinction was crazy (you snipped the part about the heap and the
    stack). Perhaps that's why Tad believes it sounds familiar.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
    Keith Keller, Sep 26, 2006
    #16
  17. <> wrote:

    > Is it advisable to think about these issues and consider them when
    > actually doing programming?



    Yes. Much of the time perl's DWIMer will shield you from needing
    to know the difference.

    But having to qualify it with "Much" implies that some times the
    distinction is important.

    A rather contrived example:

    -----------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $numList = nums_list();
    print "list: $numList\n";

    my $numArray = nums_array();
    print "array: $numArray\n";

    sub nums_list { return 4, 5, 6 }
    sub nums_array {my @nums=(4, 5, 6); return @nums}
    -----------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 26, 2006
    #17
  18. Robert Suyemoto

    Uri Guttman Guest

    >>>>> "d" == dufus <> writes:

    d> wrote:
    >> Uri Guttman wrote:
    >>
    >> > did you even read the differences i listed?

    >>
    >> Of course. No reason to get rude.


    d> Oh there's every reason to be rude. This is Uri "Motherfucker"
    d> Guttman. He is the supreme asshole of this group. To be fair to
    d> the bastard, I have to admit he has every reason to be bitter - I

    how kind and sweet of you to reply. anonymous posts are so honest and
    heartwarming. and your perl skills are renowned throughout the land
    too. and such poetry and prose you have composed over the centuries. you
    are so famous and yet you deign to write to me personally. i am honored.

    and there are cures for vaginal infections. you don't have to be a
    recluse anymore. just see a doctor, preferably a headshrinker.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 26, 2006
    #18
  19. Robert Suyemoto

    Ch Lamprecht Guest

    Tad McClellan wrote:
    > <> wrote:
    >
    >
    >>Is it advisable to think about these issues and consider them when
    >>actually doing programming?

    >
    >
    >
    > Yes. Much of the time perl's DWIMer will shield you from needing
    > to know the difference.
    >
    > But having to qualify it with "Much" implies that some times the
    > distinction is important.
    >
    > A rather contrived example:
    >
    > -----------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $numList = nums_list();
    > print "list: $numList\n";
    >
    > my $numArray = nums_array();
    > print "array: $numArray\n";
    >
    > sub nums_list { return 4, 5, 6 }
    > sub nums_array {my @nums=(4, 5, 6); return @nums}
    > -----------------------------
    >
    >


    This example illustrates context propagation to the return statement and
    behaviour of the 'comma' operator in scalar context. There is no list at all.

    Christoph

    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Sep 26, 2006
    #19
  20. Robert Suyemoto

    Guest

    Ch Lamprecht wrote:
    > There is no list at all.
    >


    ?
    , Sep 26, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    720
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    474
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    672
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    382
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,272
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page