Re: Inverting a hash

2010-05-09 Thread Uri Guttman
> "HP" == Harry Putnam writes: >> ARRAY(0x91af588) convol5.pnm HP> Finally my google strings hit paydirt and I discovered the use of HP> @{ $h{$name} } HP> To get at the content of what is only a reference to a hash. `@' as HP> used above is said to `dere

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Jim Gibson writes: > my @original_keys = @{$inv_hash{$inverted_key}}; > > The element of the inverted hash is an array reference. The array is fetched > (copied into @original_keys) by de-referencing the reference. If there was > only key with the value $invereted_key, then the array @original_ke

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Harry Putnam writes: > [...] > > ARRAY(0x91af588) convol5.pnm > exits only in rh1 > --- --- --- > ARRAY(0x91aeb38) .arch-inventory > exits only in rh1 > > [...] > > How can I get the actual name represented by `ARRAY(0x91af588)' etc? > Is Da

Re: Inverting a hash

2010-05-09 Thread Jim Gibson
On 5/9/10 Sun May 9, 2010 1:02 PM, "Harry Putnam" scribbled: > Shawn H Corey writes: > > >> my %inv_hash = invert( \%hash ); >> print '%inv_hash: ', Dumper \%inv_hash; >> >> This will output: >> >> %inv_hash: $VAR1 = { >> 'f2' => [ >> './b/l/c/f2' >> ], >> 'fb' => [ >> '.

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Shawn H Corey writes: > my %inv_hash = invert( \%hash ); > print '%inv_hash: ', Dumper \%inv_hash; > > This will output: > > %inv_hash: $VAR1 = { > 'f2' => [ > './b/l/c/f2' > ], > 'fb' => [ > './b/fb', > './b/g/h/r/fb' > ], > 'fc' => [ > './b/g/f/r/fc' > ], > 'fd

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
"Uri Guttman" writes: > as others have shown, that fails because of the dup value of 'fb'. but > in some cases where you know there are no dups, then reverse is a fine > solution. it all depends on the data and what you want to see when you > invert the hash. Well give the devil his due... you a

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Dermot writes: [...] > my %inverse_hash = invert( \%hash ); > > invert() is being passed a reference to the hash. References are a > useful and efficient way to pass data around [1]. You can create a > reference by putting a back-slash in front of your data. EG: [...] snipped more good info T

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Shawn H Corey writes: First, let me thank you for explaining every one of my questions simply and fully. Really nice to know there are people who seem to understand so much of this. [...] > my %inv_hash = invert( \%hash ); > print '%inv_hash: ', Dumper \%inv_hash; > > This will output: > >

Re: Inverting a hash

2010-05-09 Thread Uri Guttman
> "B" == Brian writes: >> I have an example from Shawn C. from another thread, presented here >> out of context. The code does just what he meant it to do. >> It inverts a hash. B> Just curious if there is a particular reason Shawn did not use the reverse function: B> my %hash

Re: Inverting a hash

2010-05-09 Thread Brian
I have an example from Shawn C. from another thread, presented here out of context. The code does just what he meant it to do. It inverts a hash. Just curious if there is a particular reason Shawn did not use the reverse function: my %hash = ( './b/fb'=> 'fb', './

Re: Inverting a hash

2010-05-09 Thread Randal L. Schwartz
> "Brian" == Brian writes: Brian> Just curious if there is a particular reason Shawn did not use the reverse function: Brian> my %hash = ( Brian> './b/fb'=> 'fb', Brian> './b/c/fd' => 'fd', Brian> './b/l/c/f2'=> 'f2', Brian> './b/g/

Re: Inverting a hash

2010-05-09 Thread Shawn H Corey
Brian wrote: I have an example from Shawn C. from another thread, presented here out of context. The code does just what he meant it to do. It inverts a hash. Just curious if there is a particular reason Shawn did not use the reverse function: my %hash = ( './b/fb'=> 'fb

Re: Inverting a hash

2010-05-09 Thread Brian
> I have an example from Shawn C. from another thread, presented here > out of context. The code does just what he meant it to do. > It inverts a hash. Just curious if there is a particular reason Shawn did not use the reverse function: my %hash = ( './b/fb'=> 'fb',

Re: Inverting a hash

2010-05-09 Thread Dermot
On 9 May 2010 14:26, Harry Putnam wrote: > I have an example from Shawn C. from another thread, presented here > out of context.  The code does just what he meant it to do. > It inverts a hash. > > I'm trying to understand what is going on inside the sub function > `invert()'. > > ---        -

Re: Inverting a hash

2010-05-09 Thread Shawn H Corey
Harry Putnam wrote: I have an example from Shawn C. from another thread, presented here out of context. The code does just what he meant it to do. It inverts a hash. I'm trying to understand what is going on inside the sub function `invert()'. ---- ---=--- -

Re: Inverting a hash

2010-05-09 Thread Harry Putnam
Harry Putnam writes: [...] Typo alert: > It appears the parts to are again flattened into ^a hash > an array -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Inverting a hash

2010-05-09 Thread Harry Putnam
I have an example from Shawn C. from another thread, presented here out of context. The code does just what he meant it to do. It inverts a hash. I'm trying to understand what is going on inside the sub function `invert()'. ---- ---=--- - m

Re: Inverting a hash safely

2009-08-04 Thread Shawn H. Corey
into the markup, which now has to be extracted (and you already know my opinion on clever :). To me, inverting a hash is just another round of Perl Golf, not something I would use in Real Code. ;) -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organizatio

Inverting a hash safely

2009-08-04 Thread Ed Avis
x27;t really mean to suggest that any code dealing with hashes should always keep both the hash and its inverse under all circumstances. So the discussion might be getting a bit silly. >As for inverting the return from a database, I would get the database to >do it. Oh, absolutely. >&g

Re: Inverting a hash safely

2009-08-04 Thread Shawn H. Corey
e database to do it. It can take advantage of relationships inside the database that are lost when the data is extracted. So I don't think it is fair to say that inverting a hash is never needed, although you can certainly minimize the need for it if you build your own data structures carefull

Re: Inverting a hash safely

2009-08-04 Thread Ed Avis
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

Re: Inverting a hash safely

2009-08-04 Thread Ed Avis
It's a question of taste, but I would prefer to just get one hash as the return value, and invert it if I need to. So I don't think it is fair to say that inverting a hash is never needed, although you can certainly minimize the need for it if you build your own data structures careful

Re: Inverting a hash safely

2009-08-04 Thread Ed Avis
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

Re: Inverting a hash safely

2009-08-03 Thread Shawn H. Corey
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

Re: Inverting a hash safely

2009-08-03 Thread Jenda Krynicky
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

Re: Inverting a hash safely

2009-08-03 Thread Uri Guttman
> "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

Re: Inverting a hash safely

2009-08-03 Thread 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 think it's simple enough: #!/usr/bin/perl use strict;

Re: Inverting a hash safely

2009-08-03 Thread Jenda Krynicky
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'

Re: Inverting a hash safely

2009-08-03 Thread 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's a need. > >my %hash = (a => 1,

Re: Inverting a hash safely

2009-08-03 Thread Jenda Krynicky
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

Inverting a hash safely

2009-08-03 Thread Ed Avis
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: