Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Ending up with duplicate keys in a hash?

by tangent (Parson)
on Jan 11, 2016 at 00:29 UTC ( [id://1152442]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Ending up with duplicate keys in a hash?
by choroba (Cardinal) on Jan 11, 2016 at 00:40 UTC
    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
Node Status?
node history
Node Type: note [id://1152442]
help
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found