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

I don't understand what you mean. Sure it is possible to modify a list! Just like in C, I can pass a reference to a list to be modified and it will be modified! In the code below, I am modifying the actual list since the sub uses the reference to that list.
#!usr/bin/perl -w use warnings; sub modify_list { my $listRef = shift; @$listRef = grep{!/^b/}@$listRef; #note there is no "return" value here #the list has been modified!!!! } my @list = ("a1", "b23", "c45", "d1", "b43", "b65"); print join (" ",@list),"\n"; #prints: a1 b23 c45 d1 b43 b65 modify_list(\@list); print join (" ",@list),"\n"; #prints a1 c45 d1

Replies are listed 'Best First'.
Re^10: Perl vs C
by ikegami (Patriarch) on Mar 16, 2009 at 15:34 UTC

    You've only modified array @list again. No lists were modified.

    You're also wrong about there being no return value. It's just being ignored.

      1. Yes all Perl subs have a return value. Correct.

      2. Again we are into semantics. I modified the data structure that was passed into the sub by reference. So write some code that modifies a "list". What would that be? Some authors say that @xyz is a list and that xyz is an array variable that defines a list. What's wrong with saying that @xyz is a list? Or that xyz in the context of @ is a list? Or that (@xyz >1) is a list in a scalar context?

        So write some code that modifies a "list". What would that be?

        We're still waiting for you to show it's possible.

        Some authors say that @xyz is a list and that xyz is an array variable that defines a list.

        So far, I've only seen you, and that's the problem.

        What's wrong with saying that @xyz is a list?

        It's very confusing. It leads to contradictory statements. If we went by your definition, all of the following statements are true depending on whether you are talking about (foo,bar) or @foo.

        • A list can't be modified.
        • A list can be modified.
        • A list in scalar context evaluates to its last item.
        • A list in scalar context evaluates to the number of elements it contains.
        • Nested lists are flattened into a single list.
        • Nested lists aren't flattened into a single list.

        [What's wrong with saying] that xyz in the context of @ is a list?

        Now you want to redefine "context" too! What do you have against being understood.

        [What's wrong with saying] that (@xyz >1) is a list in a scalar context?

        You wouldn't be talking about Perl. In Perl, lists evaluate to their last element in scalar context.

        print(scalar( ( 'a', 'b', 'c' ) )); # c

        However, @xyz does not.

        @xyz = ( 'a', 'b', 'c' ); print(scalar( @xyz )); # 3
Re^10: Perl vs C
by chromatic (Archbishop) on Mar 16, 2009 at 18:13 UTC
    Just like in C, I can pass a reference to a list to be modified and it will be modified!

    Please demonstrate, with working and tested Perl, how to take a reference to a list without using an array. At that point, I will believe that Perl supports mutable first-class lists as data structures (instead of multiple items on the Perl stack or a series of comma-separated expressions in source code).