Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > delete() on multi level hash

Reply
Thread Tools

delete() on multi level hash

 
 
moritz.maisel@googlemail.com
Guest
Posts: n/a
 
      02-23-2007
Hi,

how (if at all) is the behaviour of delete() defined for "multi-level-
hash-references"?

What I expected from the code at the bootom was:

$VAR1 = {
'y' => {
'8' => {
'c' => 3,
}
},
'w' => {
'' => {},
'6' => {
'a' => 1,
}
},
'x' => {
'7' => {
'b' => 2,
}
},
'z' => {
'9' => {
'd' => 4,
}
}
};

But I got:

$VAR1 = {
'y' => {
'' => {},
'8' => {
'c' => 3,
'foo' => 'y'
}
},
'w' => {
'' => {},
'6' => {
'a' => 1,
'foo' => 'w'
}
},
'x' => {
'' => {},
'7' => {
'b' => 2,
'foo' => 'x'
}
},
'z' => {
'' => {},
'9' => {
'd' => 4,
'foo' => 'z'
}
}
};

It works as expected if I copy the reference to a helper variable and
doing the delete on that, but I would like to understand the behaviour
in the described case ...

Does anybody have an explanation? Or a hint where to find one on the
net?

Thanks in advance,
Moritz

-----

#!/usr/bin/perl

use Data:umper;

$list = [
{ "foo" => "w", "bar" => "6" , "a" => 1 } ,
{ "foo" => "x", "bar" => "7" , "b" => 2 } ,
{ "foo" => "y", "bar" => "8" , "c" => 3 } ,
{ "foo" => "z", "bar" => "9" , "d" => 4 }
];

print Dumper $list;


foreach (@{$list}) {
$temp->{$_->{'foo'}}->{$_->{'bar'}} = $_;
delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'bar'});
delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'foo'});
}

print Dumper $temp;

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      02-23-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> how (if at all) is the behaviour of delete() defined for "multi-level-
> hash-references"?
>
> What I expected from the code at the bootom was:
>
> $VAR1 = {
> 'y' => {
> '8' => {
> 'c' => 3,
> }
> },


[ snip ]

> };
>
> But I got:
>
> $VAR1 = {
> 'y' => {
> '' => {},
> '8' => {
> 'c' => 3,
> 'foo' => 'y'
> }
> },


[ snip ]

> };
>
> It works as expected if I copy the reference to a helper variable and
> doing the delete on that, but I would like to understand the behaviour
> in the described case ...
>
> Does anybody have an explanation? Or a hint where to find one on the
> net?
>
> #!/usr/bin/perl
>
> use Data:umper;
>
> $list = [
> { "foo" => "w", "bar" => "6" , "a" => 1 } ,
> { "foo" => "x", "bar" => "7" , "b" => 2 } ,
> { "foo" => "y", "bar" => "8" , "c" => 3 } ,
> { "foo" => "z", "bar" => "9" , "d" => 4 }
> ];
>
> print Dumper $list;
>
>
> foreach (@{$list}) {
> $temp->{$_->{'foo'}}->{$_->{'bar'}} = $_;


(Using the first element of @$list.)

$temp->{ "w" }->{ "6" } = { "foo" => "w", "bar" => "6", "a" => 1 };


> delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'bar'});


delete( $temp->{ "w" }->{ "6" }->{ 'bar' } );

Which leaves $temp with:

$temp->{ "w" }->{ "6" } = { "foo" => "w", "a" => 1 };


> delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'foo'});


Since you just deleted the key 'bar' this becomes:

delete( $temp->{ "w" }->{ "" }->{ 'foo' } );

Autovivification creates the "" key and there is no 'foo' under that key to
delete.


> }


You probably want something like:

my $temp;
for ( @$list ) {
$temp->{ delete $_->{ foo } }{ delete $_->{ bar } } = $_;
}
print Dumper $temp;




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-23-2007
On Feb 23, 5:59 am, "(E-Mail Removed)"
> foreach (@{$list}) {
> $temp->{$_->{'foo'}}->{$_->{'bar'}} = $_;


Just to add to what John said - do you realize that after this
statement, both $_ and $temp->{w}->{6} are references to the *same*
hash? That changes to one affect the other?

> delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'bar'});


So when you remove the key 'bar' from the hash referenced by $temp-
>{w}->{6}, you're also removing it from the hash referenced by $_.


> delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'foo'});


Which means at this point that the hash referenced by $_ does not have
the key 'bar' anymore. That's why you're getting the results you were
seeing.

Paul Lalli

 
Reply With Quote
 
moritz.maisel@googlemail.com
Guest
Posts: n/a
 
      02-27-2007
Hi John and Paul!

Thanks a lot for your detailed explanations!

Best regards,
Moritz

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Accessing higher security level from higher security level nderose@gmail.com Cisco 0 07-11-2005 10:20 PM
rearrange "columns" of a multi-level hash? hymie! Perl Misc 5 06-15-2004 12:48 PM



Advertisments