Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

(jeffa) Re: Unconstructive Criticism

by jeffa (Bishop)
on May 27, 2003 at 14:30 UTC ( [id://260998]=note: print w/replies, xml ) Need Help??


in reply to A Quote
in thread Object to Map Multiple Values to a Single Key

Regardless of tone, Abigail-II is correct. You should have based your object from Tie::Hash instead: (update: added DELETE)
package Tie::Hash::MultiVal; use strict; use warnings; use Tie::Hash; use base qw(Tie::StdHash); sub TIEHASH { my $class = shift; return bless {@_}, $class; } sub STORE { my ($hash,$k,$v) = @_; if (exists $hash->{$k}) { if (ref $hash->{$k}) { push @{$hash->{$k}}, $v; } else { $hash->{$k} = [$hash->{$k},$v]; } } else { $hash->{$k} = $v; } } sub DELETE { my ($hash,$k) = @_; if (ref $hash->{$k}) { pop @{$hash->{$k}}; $hash->{$k} = $hash->{$k}[0] if @{$hash->{$k}} == 1; } else { delete $hash->{$k}; } } 1;
Now i can use this to solve the 'problem' i had over at URI.pm bug or am i missing something? ... notice the slice ;)
use strict; use warnings; use URI; use Data::Dumper; use Tie::Hash::MultiVal; my $uri = URI->new('http://foo.com/bar.cgi?foo=bar&foo=baz&bar=qux&baz +=qux'); my %hash; tie %hash, 'MyHash'; %hash = $uri->query_form(); @hash{qw(foo bar baz qux)} = qw(one two three four); delete $hash{foo}; print Dumper \%hash;

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

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

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

    No recent polls found