Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Ending up with duplicate keys in a hash?\

by betterworld (Curate)
on Jan 10, 2016 at 22:56 UTC ( #1152437=note: print w/replies, xml ) Need Help??


in reply to Ending up with duplicate keys in a hash?\

Just a little detail I noticed... I am not sure if this is relevant or maybe even intended, but your event_hasher function has non-obvious side effects:

$_ =~ s/://g;

This will modify the event data structure because $_ is a writable alias into @event.

Replies are listed 'Best First'.
Re^2: Ending up with duplicate keys in a hash?
by tangent (Vicar) on Jan 11, 2016 at 00:29 UTC
    I noticed that too and not sure if it is relevant, but I have been stung by something similar in the past. I would re-write the subroutine as:
    my $type = event_hasher(\@event); sub event_hasher { my $cur_event = shift; my %event_hash; for my $item (@$cur_event) { $item =~ s/://g; my ($key,$value) = split / /, $item; $event_hash{$key} = $value; } return \%event_hash; } $queue_changes{$event_id} = $type;

    Update:
    As choroba points out below this doesn't solve the problem - in fact it creates it! You need to make a copy of the input - but you are doing that anyway so the aliasing is not the problem. Using choroba's test with your subroutine:

    sub event_hasher { my @cur_event = @{$_[0]}; my %event_hash; foreach(@cur_event){ $_ =~ s/://g; my @place_holder = split / /, $_; $event_hash{$place_holder[0]} = $place_holder[1]; } return %event_hash; } my @event = ('a:b c:d'); print "@event\n"; my %type = event_hasher(\@event); print "@event\n"; # prints: a:b c:d a:b c:d
      The problem remains: $item is still an alias to the members of the @event.
      #!/usr/bin/perl use warnings; use strict; sub event_hasher { my $cur_event = shift; my %event_hash; for my $item (@$cur_event) { $item =~ s/://g; my ($key, $value) = split / /, $item; $event_hash{$key} = $value; } return \%event_hash } my @event = ('a:b c:d'); print "@event\n"; my $type = event_hasher(\@event); print "@event\n";
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-12-05 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?