Jeff Abrahamson on 13 Dec 2004 01:28:47 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: perldb weirdness


Hmm.  Originally I had written

    $unique_sets{$sets{$s}} = 1;

I guess that made one copy and now I make two copies.

What I want to do is keep referring to the same object.  Is there an
easy idiom for this?  I had thought references would do it.

The point is that I have a set of hash references.  From time to time
I realize that two of them, h1 and h2, really should be the same, so I
coalesce their data into h1 and then set h2 = h1.  (Recall that they
are references to hashes, not hashes.)

I'm probably thinking overly C-like.

Here's the fuller code section that shows me doing what I just said.
The key part is the block beginning with the comment "union of sets".

    my %sets = ();
    for my $v (keys %V) {
	$sets{$v} = [$v];
    }
    for my $v_begin (keys %E) {
	my $adj = $E{$v_begin};
	for my $v_end (keys %{$adj}) {
	    if($sets{$v_begin} != $sets{$v_end}) {
		# union of sets
		my %s1 = %{$sets{$v_begin}};
		my %s2 = %{$sets{$v_end}};
		for my $key (keys %s1) {
		    $s2{$key} = 1;
		}
		$sets{$v_begin} = $sets{$v_end};   # set pointers equal
	    }
	}
    }
    my %unique_sets;
    for my $s (keys %sets) {
	print $sets{$s}, "\n";
	$unique_sets{$sets{$s}} = $sets{$s};
	print $sets{$s}, "\n";
    }

-Jeff



On Sun, Dec 12, 2004 at 07:59:27PM -0500, Eric Roode wrote:
>   [62 lines, 224 words, 1590 characters]  Top characters: es_\nto>r
> 
> No, you have two different references to the same array.  This line:
> 
>     $unique_sets{$sets{$s}} = $sets{$s};
> 
> makes a copy of the reference, not a copy of the array to which the reference
> points.
> 
> HTH
> 
> Quoting Jeff Abrahamson <jeff@purple.com>:
> 
> > Notice how I appear to have two different arrays with base address
> > 0x8522038.  Does this make sense to anyone here?
> > 
> >       DB<64> @k=keys %unique_sets
> > 
> >       DB<65> $k=$k[0]
> > 
> >       DB<66> x $k
> >     0  'ARRAY(0x8522038)'
> >       DB<67> x $sets{$s}
> >     0  ARRAY(0x8522038)
> >        0  'jpw'
> >       DB<68> x @{$k}
> >       empty array
> >       DB<69> x $unique_sets{$k}
> >     0  ARRAY(0x8522038)
> >        0  'jpw'
> >       DB<70> 
> > 
> > A snippet of the underlying code is this:
> > 
> >     my %unique_sets;
> >     for my $s (keys %sets) {
> > 	print $sets{$s}, "\n";
> > 	$unique_sets{$sets{$s}} = $sets{$s};
> > 	print $sets{$s}, "\n";
> >     }
> > 
> > -- 
> >  Jeff
> > 
> >  Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
> >  GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B
> > 
> >  A cool book of games, highly worth checking out:
> >  http://www.amazon.com/exec/obidos/ASIN/1931686963/purple-20
> > 
> 
> 
> -- 
> Eric
> 
> ___________________________________________________________________
> 
> Privacy Notice: This message has been sent via www.zoemail.net using
> patented e-mail protection technology developed by AT&T Labs. Reply to
> the "keyed address" above to ensure delivery.
> 
> -
> **Majordomo list services provided by PANIX <URL:http://www.panix.com>**
> **To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

 A cool book of games, highly worth checking out:
 http://www.amazon.com/exec/obidos/ASIN/1931686963/purple-20

Attachment: signature.asc
Description: Digital signature