Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

assignment to hash with duplicates in list context (bug?)

by LanX (Saint)
on Sep 25, 2013 at 00:44 UTC ( [id://1055576]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

(update: please note Already discussed)

ehm I think I saw this effect before, but can someone explain the values in @list?

DB<126> \%count => { 3 => 1, 5 => 1, 10 => 3 } DB<127> @list= %rcount= reverse %count => (3, 10, 3, 10) DB<128> \%rcount => { 1 => 3, 3 => 10 } DB<129> \@list => [3, 10, 3, 10]

to exclude problems with my repl

lanx@nc10-ubuntu:~$ perl -MData::Dumper -e ' %h1=(3 => 1, 5 => 1, 10 = +> 3 ); @l=%h2=reverse %h1; print Dumper \@l,\%h2' $VAR1 = [ 3, '10', 3, '10' ]; $VAR2 = { '1' => '3', '3' => '10' }; lanx@nc10-ubuntu:~$ perl -version This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

bug or undefined behavior? reproducible in which versions?

update
lanx@nc10-ubuntu:~$ perl -MData::Dumper -e ' %h1=(3 => 1, 5 => 1, 10 = +> 3 ); %h4=%h3=%h2=reverse %h1; print Dumper \%h2,\%h3,\%h4' $VAR1 = { '1' => '3', '3' => '10' }; $VAR2 = { '3' => '10' }; $VAR3 = { '3' => '10' };

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re: assignment to hash with duplicates in list context (bug?)
by kcott (Archbishop) on Sep 25, 2013 at 02:04 UTC

    G'day LanX,

    I can't reproduce this with v5.18.1 (darwin-thread-multi-2level):

    $ perl -MData::Dumper -e ' %h1=(3 => 1, 5 => 1, 10 => 3 ); @l=%h2=reve +rse %h1; print Dumper \@l,\%h2' $VAR1 = [ 1, '3', 3, '10' ]; $VAR2 = { '1' => '3', '3' => '10' }; $ perl -MData::Dumper -e ' %h1=(3 => 1, 5 => 1, 10 => 3 ); %h4=%h3=%h2 +=reverse %h1; print Dumper \%h2,\%h3,\%h4' $VAR1 = { '1' => '3', '3' => '10' }; $VAR2 = { '1' => '3', '3' => '10' }; $VAR3 = { '1' => '3', '3' => '10' };

    In v5.18.0, there were changes to hashes (see perl5180delta: Hash overhaul). This meant that running these one-liners (potentially) produced different results each time. I ran these a few times: I got different results as expected but none reproduced what you're seeing. Here's some examples of this:

    -- Ken

Re: assignment to hash with duplicates in list context (bug?)
by tinita (Parson) on Sep 25, 2013 at 01:16 UTC
    also funny:
    my @count = qw/ 3 1 5 1 10 3 /; my @list= my %rcount = reverse @count; say "@list" __END__ 1 1 1 1
    both cases seem to be fixed at least since 5.14.2, but I can't find anything in perldelta
Re: assignment to hash with duplicates in list context (accidents)
by tye (Sage) on Sep 25, 2013 at 01:19 UTC

    I like these variations:

    say "@a = %h = ( a=>1, b=>2, c=>3, d=>4, a=>5 )" b 2 b 2 c 3 d 4 say "@a = %h = ( a=>1, b=>2, c=>3, d=>4, b=>5 )" b b b b c 3 d 4 say "@a = %h = ( a=>1, b=>2, c=>3, d=>4, c=>5 )" b 2 b 2 c b d 4 say "@a = %h = ( a=>1, b=>2, c=>3, d=>4, d=>5 )" b 2 b 2 c 3 d b

    As for explanation, I find it an unimportant quirk. I've never seen the desired behavior defined. I could guess at a few behaviors that I might expect. I'd hate for something to be made less efficient for the sake of making this particular construct return something less surprising.

    I'd be fine with only the case of @a = %h = ... being made less efficient by translating it into: @a = do { %h = ...; %h }.

    There have been several times where I have used %h = @a = ..., and the behavior of that is well defined. That line is usually followed by code to pull out just the keys from @a because I care about the order in which the keys were given.

    The current return values hint that an attempt might have been made to make this return what %h would return except preserving the original order of the items as much as possible. That would be an interesting result that might even be useful. If that can be achieved by relatively simple work that doesn't adversely impact efficiency or code complexity, then I'd be in favor of that.

    My admittedly wild guess is that the return values might actually be the result of trying to get the construct to return:

    this: a 1 c 3 d 4 b 5 not: a 1 b 5 c 3 d 4

    or to avoid it returning

    a 1 b *random* c 3 d 4 b 5

    and perhaps even *random* potentially resulting in core dumps. :)

    But my favorite result would be for it return:

    this: a b c d or: a c d b or: a b c d b

    - tye        

      The usability of

      @l=%h2=reverse %h1;

      may be debatable ...

      But %h2 identical to %h3 in

      %h3=%h2=reverse %h1;

      is IMHO normal expected behavior.

      The problem is related to collisions, no collisions no problems.

      lanx@nc10-ubuntu:~$ perl -MData::Dumper -e ' @h1=(1 => 3, 2 => 5, 3 => + 10); %h3=%h2=@h1; print Dumper \%h2,\%h3' $VAR1 = { '1' => 3, '3' => 10, '2' => 5 }; $VAR2 = { '1' => 3, '3' => 10, '2' => 5 };

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: assignment to hash with duplicates in list context (bug?)
by choroba (Cardinal) on Sep 25, 2013 at 01:14 UTC
    Seems ok in 5.16.0.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Already discussed
by LanX (Saint) on Sep 25, 2013 at 01:45 UTC

Log In?
Username:
Password:

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

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

    No recent polls found