Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: Adding Unique Elements to Array

by ihb (Deacon)
on Mar 01, 2005 at 03:19 UTC ( [id://435268]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Adding Unique Elements to Array
in thread Adding Unique Elements to Array

The fact is that your x= tricks works as just as much as the undef() because of undocumented behaviour. This is shown by

my $foo; sub context :lvalue { $foo = wantarray ? 'list' : 'scalar' } context() x= 1; print $foo; __END__ scalar
or using simpler means:
use Data::Dumper; my %foo = qw/ a 1 b 2 /; @foo{qw/ a b /} x= 0; print Dumper \%foo; __END__ $VAR1 = { 'a' => '1', 'b' => '' };
A trick that does work though is the reference trick. All these other solutions work because of autovivification. So let's use \ whose behaviour explicitly is documented.
\@foo{@list};

ihb

See perltoc if you don't know which perldoc to read!

Replies are listed 'Best First'.
Re^6: Adding Unique Elements to Array
by Roy Johnson (Monsignor) on Mar 01, 2005 at 03:43 UTC
    The fact is that your x= tricks works is just as much as the undef() because of undocumented behaviour.
    No, the behavior of assignment and the x operator are defined for array slices. If you don't like the zero, one works just as well.

    That said, I like the ref trick. It's fast, documented, and not too bizarre. ++

    I've thought about it a little more, and I wonder whether it actually is documented behavior. Is \ documented for array slices? Dumper tells me it's behaving like \(), making a list of references, which is sensible. I guess a slice is a list, even without surrounding parentheses...


    Caution: Contents may have been coded under pressure.

      No, the behavior of assignment and the x operator are defined for array slices.

      Both () x EXPR and () = EXPR are defined, but () x= EXPR is another operator and doesn't work like you'd expect; in fact, () x= isn't documented and x is the only operator that can be used with = that has special semantics for its LHS operand. All other force scalar context so it's a no-brainer how they'll work for list assignment. perlop says that it works like in C, and you can then assume the following paragraph is about scalar assignment and it always talks about a singular lvalue. Only in the last paragraph list assignment is covered.

      In (context()) = EXPR the subroutine &context is called in list context; in (context()) x= EXPR it's called in scalar context. However, the lvalue of all elements in the list in (LIST) x= EXPR are taken (hence the autovivification), just as for

      sub foo {} foo(@foo{qw/a b/}); print keys %foo; # ab
      The lvalues are then put in scalar context making them not a list anymore but a sequence of scalars with the scalar-comma between them (see perlop). Why this happens I have no idea. As a consequence of the "list" of elements being "comma-separated in scalar context" all values but the last are put in void context and the last in scalar context. Example:
      use warnings; sub context { defined $_[0] ? $_[0] ? 'list' : 'scalar' : 'void' } sub foo :lvalue { print context(wantarray); my $dummy = '' } sub bar :lvalue { print context(wantarray); my $dummy = '' } my $baz; ($baz, foo(), bar()) x= 3; __END__ Useless use of private variable in void context void scalar

      Is \ documented for array slices?

      From perlref

      Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

      The explicit parenthesis isn't needed. (Just consider \qw/ foo bar /.) @foo[@bar] is a listop called aslice. I don't know if it's documented that aslice may be used instead of a list, so you may be right. It's at least more documented than the other tricks. ;-)

      ihb

      See perltoc if you don't know which perldoc to read!

        You are right. This gets a compilation error:
        my @foo = qw(one two three); @foo x= 3;
        I had thought that all combination operators were defined in terms of their expansion: if you can write @foo = @foo x 3, you can write @foo x= 3. Not so. Good to know.

        Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://435268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-18 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found