in reply to Removing duplicates in Array of Array

Oh, and your array initialization is completely broken.

I thought (like GrandFather above) that there was something wrong with

my @array; push @{@array},["1", "1", "3"]; push @{@array},["1", "5", "6"]; push @{@array},["1", "1", "3"];

But your code runs fine, even using strict, and generates no warnings.

I wondered if the @array inside the curlies was being evaluated in scalar context or as the literal '@array', but no. As far as I can tell with Data::Dumper, these statements populate @array.

Never mind what you were trying to do, can any of our brethren explain what you've actually done?

Replies are listed 'Best First'.
Re: Amazing syntax of push (was Re: Removing duplicates in Array of Array)
by BrowserUk (Patriarch) on Sep 04, 2008 at 07:07 UTC

    Seems you do not need either push or anonymous arrays to see the behaviour with 5.8.6:

    c:\test>perl -Mstrict -MData::Dump=pp -wle"my @r=1..10; @{ @r }='a'..'c'; pp \@r" ["a", "b", "c"]

    @{ @r } seems to function exactly the same as either @r alone (or @{ \@r }). Which is somewhat bizarre.

    But not so with 5.10

    c:\test>\perl510\bin\perl -MData::Dump=pp -wle"my @r=1..10; @{ @r }='a'..'c'; pp \@r" [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] c:\test>\perl510\bin\perl -Mstrict -MData::Dump=pp -wle"my @r=1..10; @{ @r }='a'..'c'; pp \@r" Can't use string ("10") as an ARRAY ref while "strict refs" in use at +-e line 1.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I see that I missed a step: run the program with perl 5.10.0. So, the program parses|compiles ok but causes run time error as above (in my example 10 in above error message is a 2). For the record, perl 5.8.8 produces neither compile time nor run time error.
Re: Amazing syntax of push (was Re: Removing duplicates in Array of Array)
by parv (Parson) on Sep 04, 2008 at 05:14 UTC

    The seemingly obvious reasons that I can come up with are (a) either array in push @{ @p } , ... is magically being used as an array reference; (b) or the array deference is being optimized out. Given ...

    use warnings; use strict; my @p; my $p = \@p; push @p , [ 1 , 2 ]; push @{ $p } , [ 3 , 4 ]; push @{ @p } , [ 5 , 6 ];

    ... output of perl -MO=Terse file (5.8.8; in case somebody can make more sense of it than I can) ...

    perl 5.8.8 & 5.10.0 did not have any problems in parsing the code.

      rv2av doesn't seem to care whether it gets an array reference or an array.

      Update: This is confirmed by BrowserUK's less specific test and by looking at the 5.8.8 source:

      PP(pp_rv2av) { ... if (SvROK(sv)) { ... av = (AV*)SvRV(sv); ... } else { if (SvTYPE(sv) == SVt_PVAV) { av = (AV*)sv; ... } ... } ... ...return something derived from av... }
Re: Amazing syntax of push (was Re: Removing duplicates in Array of Array)
by GrandFather (Saint) on Sep 04, 2008 at 05:02 UTC

    When I run that code using Perl v5.8.7 I get the error:

    Bizarre copy of ARRAY in leave at C:\Delme~~\PerlScratch\noname.pl lin +e 2. Is this a version issue?

    without strictures.


    Perl reduces RSI - it saves typing