coffeemaster1 has asked for the wisdom of the Perl Monks concerning the following question:
2). It is then categorized/sanitized in a series of essentially inconsequential conditionals and is converted into a hash for easier parsing (at least by my current method) later:while (1){ my @event = $astman->read_response; chomp(@event);
event_hasher:lock(%queue_changes); $event_id = int(rand(25000)); %type = &event_hasher(\@event);
3). It is then put in a shared hash which consists of changes and is then read by the main thread which will update the changes: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; }
It seems that somewhere between the event being "hashed" and the newly "hashed" event being put into the hash of changes, some of the event hash keys were duplicated (at least according to Data::Dumper) as you can see below (removing some personal details): Initial Event:$queue_changes{$event_id} = %type;
"Hashed" Event:Sat Jan 9 21:10:50 2016 1452391850.044984517 Event: $VAR1 = [ 'Event: QueueMemberStatus', 'Privilege: agent,all', 'Queue: test-q', 'Location: SIP/EXTENSION-XXXX', 'MemberName: SIP/EXTENSION-XXXX', 'Membership: dynamic', 'Penalty: 0', 'CallsTaken: 1', 'LastCall: 1452391850', 'Status: 1', 'Paused: 0' ];
"Queue Changes" Hash right after this event is added:Sat Jan 9 21:10:52 2016 1452391852.284037579 Hashed Version: $VAR1 = { 'Status' => '1', 'Queue' => 'test-q', 'LastCall' => '1452391850', 'CallsTaken' => '1', 'MemberName' => 'SIP/EXTENSION-XXXX', 'Location' => 'SIP/EXTENSION-XXXX', 'Event' => 'QueueMemberStatus', 'Privilege' => 'agent,all', 'Paused' => '0', 'Membership' => 'dynamic' };
This is where stuff get a little weird, this would be the same variable from the perspective of the main thread, right after is detected a change to the "Queue Changes" hash and went to update the main hash of agents:Queue Changes after Event Hashed: $VAR1 = { '9555' => { 'Event' => 'QueueMemberStatus', 'Privilege' => 'agent,all', 'Status' => '1', 'Queue' => 'test-q', 'Membership' => 'dynamic', 'Paused' => '0', 'MemberName' => 'SIP/EXTENSION-XXXX', 'CallsTaken' => '1', 'LastCall' => '1452391850', 'Location' => 'SIP/EXTENSION-XXXX' } };
You can see there are multiple duplicate keys here and I wasn't sure if this was just Data::Dumper printing things out weird as this is a shared variable, or it was actually like this. Is this possible in Perl? I assumed that if it were to happen the "second" key/value pair would overwrite the first. Could this be caused by the use of a shared variable that is being update in one thread and read in another? (I am using locks when reading and writing to the variable). Just wanted to see if anyone had any insight in to what could be going on here. Also, if needed:Sat Jan 9 21:10:52 2016 1452391852.286708251 About to make Change: $VAR1 = { '9555' => { 'Event' => 'QueueMemberStatus', 'Queue' => 'test-q', 'MemberName' => 'SIP/EXTENSION-XXXX', 'LastCall' => '1452391850', 'Event' => 'QueueMemberStatus', 'Privilege' => 'agent,all', 'Status' => '1', 'Queue' => 'test-q', 'Membership' => 'dynamic', 'Paused' => '0', 'MemberName' => 'SIP/EXTENSION-XXXX', 'CallsTaken' => '1', 'LastCall' => '1452391850', 'Location' => 'SIP/EXTENSION-XXXX' } };
Thanks!perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ending up with duplicate keys in a hash?\
by choroba (Cardinal) on Jan 10, 2016 at 10:08 UTC | |
by Anonymous Monk on Jan 10, 2016 at 15:52 UTC | |
by hotchiwawa (Scribe) on Jan 10, 2016 at 16:46 UTC | |
|
Re: Ending up with duplicate keys in a hash?
by Athanasius (Archbishop) on Jan 10, 2016 at 06:27 UTC | |
|
Re: Ending up with duplicate keys in a hash?\
by betterworld (Curate) on Jan 10, 2016 at 22:56 UTC | |
by tangent (Parson) on Jan 11, 2016 at 00:29 UTC | |
by choroba (Cardinal) on Jan 11, 2016 at 00:40 UTC | |
|
Re: Ending up with duplicate keys in a hash?\
by Anonymous Monk on Jan 10, 2016 at 17:48 UTC | |
|
Re: Ending up with duplicate keys in a hash?\
by hotchiwawa (Scribe) on Jan 10, 2016 at 14:11 UTC |