in reply to Re^3: Perl vs C
in thread Perl vs C

Please explain what you think the difference is between a Perl array and a Perl List.
Arrays are variables, lists are values. It's the same difference as between $foo and 3.
I think this just some kind of nomenclature difference.. Not any real disagreement!
Differences in nomenclature are not something to trivialize - specially not when it's about important concepts.
A simple Perl list is similar to a C char ** array (array of pointers of pointers to strings)
No, it's not. First of all, a char is not a string, but a (small) integer. Depending on usage, a C char is equivalent to a Perl integer, or a Perl string consisting of exactly one character. Secondly, in C, array elements all have the same size - all elements are integers, characters or pointers for instance. (Pointers in your example). In Perl, values of a list can have different "sizes" - or rather, different types (mixed integers, references, objects, strings, etc) - as the "size" of a value isn't a useful concept in Perl (it is in perl, but not in Perl). Third, and this is the big one, elements of an array may change (both in C and in Perl). But in Perl, lists are unmutable (of course they can be changed by perl, or by using XS - but not from a Perl POV). They are as unmutable as the value 3.

Replies are listed 'Best First'.
Re^5: Perl vs C
by Marshall (Canon) on Mar 16, 2009 at 13:54 UTC
    Yes, a char is a character. But char*, is a pointer to an character. And char** is a pointer to a pointer to a character (array).

    Ok some examples:
    in Perl:

    #!usr/bin/perl -w use warnings; my @list = ("a1", "b23", "c45", "d1"); print join (" ",@list),"\n"; #prints: a1 b23 c45 d1
    In Perl, the list "knows" how big it is.
    This is not true in C! In C we have to do stuff to help things out! Like this:
    include <stdio.h> int main () { char *list[] = {"a1", "b23", "c45", "d1",'\0'}; // list is an array of pointers to chars char **mover; for (mover=list; *mover ;mover++) printf ("%s ",*mover); printf("\n"); } // prints: a1 b23 c45 d1
    For me, the difference is obvious! The Perl version is cool!
    The alternative way in C would be to keep track of the number of items in the list array instead of a NULL value sentinel (and yes, there are more ways than than one I showed above). All ways are a pain in the rear compared with Perl!

    Perl lists may be changed at will just like in C. I don't understand this comment: "lists are unmutable". Please explain, I just don't understand what you mean.

      I don't understand this comment: "lists are unmutable". Please explain, I just don't understand what you mean.
      Try to make pure Perl program that modifies a list. Come back when you either have such a program, or when you are convince this isn't possible.
        Here is a simple thing that removes things from a Perl list that do not match a criteria.
        #!usr/bin/perl -w use warnings; my @list = ("a1", "b23", "c45", "d1", "b43", "b65"); print join (" ",@list),"\n"; #prints: a1 b23 c45 d1 b43 b65 @list = grep{!/^b/}@list; #remove things that start with b print join (" ",@list),"\n"; #prints: a1 c45 d1