Michael Styer on 13 Dec 2004 03:28:51 -0000


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

Re: perldb weirdness


I've been looking at this code wondering what the heck is going on, and I
can't make any sense of it, so I'll ask.

Jeff Abrahamson wrote:

> 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];

So the value of $sets{$v} is an array ref with a single element, namely
$v? I don't understand why you would want this, but OK, that's cool...

>     }
>     for my $v_begin (keys %E) {
> 	my $adj = $E{$v_begin};
> 	for my $v_end (keys %{$adj}) {
> 	    if($sets{$v_begin} != $sets{$v_end}) {

but here, you're comparing those arrayrefs for equality... except that
I'm sure that this comparison is in effect something like 

    if ('ARRAY(0x8522038)' != 'ARRAY(0x8198432)') {
        do stuff...
    }

isn't it? I can't think of how to check that, but is that what you want?

> 		# union of sets
> 		my %s1 = %{$sets{$v_begin}};
> 		my %s2 = %{$sets{$v_end}};

Then here, I'm surprised you can even do this at all. I tried something
similar just now and I get "Can't coerce array into hash at ./test.pl
line 26." because $sets($v_begin} is going to be an arrayref, not a
hashref, right?

> 		for my $key (keys %s1) {
> 		    $s2{$key} = 1;

I'm not sure what you're doing here; %s1 and %s2 are created and
destroyed each time through this block, and you're not doing anything
with them, so putting the keys from %s1 into %s2 wouldn't seem to make
any difference.

> 		}
> 		$sets{$v_begin} = $sets{$v_end};   # set pointers equal
> 	    }
> 	}
>     }
>     my %unique_sets;
>     for my $s (keys %sets) {
> 	print $sets{$s}, "\n";

Isn't this going to print the string 'ARRAY(0x8522038)'... is that what
you want?

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

Here you're setting up %unique_sets so that its keys are strings of the
form 'ARRAY(0x8522038)' and its values are references to the same arrays
that the values of %sets refer to. This seems rather odd...

> 	print $sets{$s}, "\n";

Printing 'ARRAY(0x8522038)' again...

>     }

So, ah, I'm mystified. Is there some wizardry going on here that I'm not
getting? What's the point of all the playing around with
'ARRAY(0x8522038)' strings directly? It seems very un-perlish to me, but
I have a distinct feeling I may not be understanding something.

Hoping for some enlightenment...

Mike



-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**