Ed Avis wrote:
Shawn H. Corey gmail.com> writes:
Why on earth would you want to invert an XML file?
What I mean is that many modules (such as XML::Twig) return data as hashes.
To me, it doesn't make any sense to return both a hash and its inverse from
the function. Instead I would return jus
Shawn H. Corey gmail.com> writes:
>>>But then again I never have to invert a hash; when I populate it, I
>>>would populate its inverse as well.
>>But in the particular case I was thinking of,
>>there was some (programmer-maintained, not user-maintained) configuration data
>>in a hash:
>>
>>
Ed Avis wrote:
Shawn H. Corey gmail.com> writes:
But then again I never have to invert a hash; when I populate it, I
would populate its inverse as well. I would build both data structures
at the same time, inserting only the data I need, where I need it.
That's often a good approach. But
Uri Guttman stemsystems.com> writes:
> EA> foreach my $k (sort keys %hash) {
>
>why the sort?
No terribly good reason; I just wanted the error messages to be deterministic.
You could speed it up a bit by not sorting and it would still work just as well,
but the error message given might in
Shawn H. Corey gmail.com> writes:
>But then again I never have to invert a hash; when I populate it, I
>would populate its inverse as well. I would build both data structures
>at the same time, inserting only the data I need, where I need it.
That's often a good approach. But in the particul
Jenda Krynicky Krynicky.cz> writes:
>
[inverting a hash but checking that no data is lost]
>>To give a really useful error message is a bit more code:
>>
>>my %reverse;
>>foreach my $k (sort keys %hash) {
>>my $v = $hash{$k};
>>if (exists $reverse{$k}) {
>>die
Jenda Krynicky wrote:
From: "Shawn H. Corey"
push @{ $r{$h{$_}} }, $_ for keys %h; # one line :)
Is it simple enough so that you immediately know what does it do? I
guess not. IMHO it's complex enough to warrant being moved to a named
subroutine. Especially since it forces you to write th
From: "Shawn H. Corey"
> Jenda Krynicky wrote:
> > And if you feel like it, create a function that reverses
> >
> > (a => 1, b => 3, c => 1) => (1 => ['a','c'], 2 => ['b'])
> >
> > That's something that's not a SIMPLE oneliner. Even though of course
> > it's not too complex either.
>
> I thin
> "EA" == Ed Avis writes:
EA> foreach my $k (sort keys %hash) {
why the sort?
uri
--
Uri Guttman -- u...@stemsystems.com http://www.sysarch.com --
- Perl Code Review , Architecture, Development, Training, Support --
- Free Perl Training --- http://p
Jenda Krynicky wrote:
And if you feel like it, create a function that reverses
(a => 1, b => 3, c => 1) => (1 => ['a','c'], 2 => ['b'])
That's something that's not a SIMPLE oneliner. Even though of course
it's not too complex either.
I think it's simple enough:
#!/usr/bin/perl
use strict;
From: Ed Avis
> Jenda Krynicky Krynicky.cz> writes:
>
> >> my %hash = (a => 1, b => 2);
> >> my %reverse = safe_hash_invert %hash; # works fine
> >>
> >> $hash{c} = 1;
> >> %reverse = safe_hash_invert %hash; # throws an error
>
> >I don't think there is and I don't think there'
Jenda Krynicky Krynicky.cz> writes:
>> my %hash = (a => 1, b => 2);
>> my %reverse = safe_hash_invert %hash; # works fine
>>
>> $hash{c} = 1;
>> %reverse = safe_hash_invert %hash; # throws an error
>I don't think there is and I don't think there's a need.
>
>my %hash = (a => 1,
From: Ed Avis
> My question is, does there exist a 'safe hash invert' function in some CPAN
> module? I was imagining something like
>
> my %hash = (a => 1, b => 2);
> my %reverse = safe_hash_invert %hash; # works fine
>
> $hash{c} = 1;
> %reverse = safe_hash_invert %hash; # thr
The standard answer on how to invert a hash, so that keys become values and vice
versa, is to use reverse:
my %reversed = reverse %hash;
The standard answer also mentions some caveats, most importantly that if the
original hash is not one-to-one, then some entries will be lost:
use Data:
14 matches
Mail list logo