perl:可以使用散列项引用从散列中删除吗?

perl:可以使用散列项引用从散列中删除吗?,perl,hash,filehandle,Perl,Hash,Filehandle,下面是一些perl伪代码,用于回答我的问题: my %x; # initialize %x my $ref = whateverSyntacticSugarIsNeeded( $x{this}{hash}{is}{deep} ); # ... # make use of $ref multiple times # ... delete $ref; # ideally, this would delete $x{this}{hash}{is}{deep} 。。。这里的想法是避免使用超过绝对

下面是一些perl伪代码,用于回答我的问题:

my %x;
# initialize %x

my $ref = whateverSyntacticSugarIsNeeded( $x{this}{hash}{is}{deep} );

# ...
# make use of $ref multiple times
# ...

delete $ref; # ideally, this would delete $x{this}{hash}{is}{deep}
。。。这里的想法是避免使用超过绝对必要的
$x{this}{hash}{is}{deep}


我相当确定这是不可能的,可能的最少使用是2(值的初始引用/副本,然后从
%x
中删除键/值对)。但是,如果我弄错了,请随时纠正我。

最简单的事情就是将哈希值向上引用一级

如果给变量一个语义上合适的名称(ref以外的名称),则尤其如此:


最简单的事情就是将散列引用一级

如果给变量一个语义上合适的名称(ref以外的名称),则尤其如此:


你到底想要什么还不清楚。如果

%x = ( very => { deep => { hash => { is => "here" } } } );
然后你分配

$y = $x{very}{deep}{hash}{is}
那就像写作一样

$y = 'here'
因此,您不能
删除$y
。但是你可以

$z = $x{very}{deep}{hash};
delete $z->{is};

你到底想要什么还不清楚。如果

%x = ( very => { deep => { hash => { is => "here" } } } );
然后你分配

$y = $x{very}{deep}{hash}{is}
那就像写作一样

$y = 'here'
因此,您不能
删除$y
。但是你可以

$z = $x{very}{deep}{hash};
delete $z->{is};

Perl通过对一段内存进行计数来跟踪该内存是否被使用。如果我这样做:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我释放了所有的记忆。所有哈希引用以及这些哈希引用所指向的实际标量。那是因为我没有进一步的方法来访问内存

如果我做一些简单一点的事情:

my %hash = ( one => 1, two => 2, ref => { three => 3, four => 4 }, five => 5 );
注意,
$hash{ref}
是对另一个hash的引用。如果我这样做:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我现在有两个变量可以访问这段内存。这样做:

delete $hash{ref};
delete $hash{ref}->{four};
不会释放该内存,因为
$ref
仍指向它。但是,该散列引用不再在我的
%hash
散列中

如果我没有删除
$hash{ref}
,但这样做了:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我更改了
%hash
,因为
$ref
$hash{ref}
指向同一块内存:同一个hash引用。这样做:

delete $hash{ref};
delete $hash{ref}->{four};
或 删除$ref->{four}

两者都将删除该散列引用中的特定条目

我们也不必做那么复杂的事情:

my %hash = ( one => 1, two => 2, three => 3 );
my $ref = \%hash;     #Creating a reference to that hash
delete $ref->{three};
这将删除$hash{three},因为两者都指向内存中的同一个hash。但是,

undef $ref;
也不会取消定义
$hash


我希望这涵盖了你的问题。只要有其他方法引用内存位置,Perl中就不会释放它。但是,如果将引用指向数据结构,则通过引用操纵该数据结构将操纵通过数组或散列变量引用的数据结构。

Perl通过使用该内存段的计数来跟踪是否使用了该内存段。如果我这样做:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我释放了所有的记忆。所有哈希引用以及这些哈希引用所指向的实际标量。那是因为我没有进一步的方法来访问内存

如果我做一些简单一点的事情:

my %hash = ( one => 1, two => 2, ref => { three => 3, four => 4 }, five => 5 );
注意,
$hash{ref}
是对另一个hash的引用。如果我这样做:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我现在有两个变量可以访问这段内存。这样做:

delete $hash{ref};
delete $hash{ref}->{four};
不会释放该内存,因为
$ref
仍指向它。但是,该散列引用不再在我的
%hash
散列中

如果我没有删除
$hash{ref}
,但这样做了:

my $ref->{this}->{is}->{a}->{deep} = "hash";
undef $ref;     # Note I use "undef" and not "delete"
my $ref = $hash{ref};
$ref->{seven} = 7;
我更改了
%hash
,因为
$ref
$hash{ref}
指向同一块内存:同一个hash引用。这样做:

delete $hash{ref};
delete $hash{ref}->{four};
或 删除$ref->{four}

两者都将删除该散列引用中的特定条目

我们也不必做那么复杂的事情:

my %hash = ( one => 1, two => 2, three => 3 );
my $ref = \%hash;     #Creating a reference to that hash
delete $ref->{three};
这将删除$hash{three},因为两者都指向内存中的同一个hash。但是,

undef $ref;
也不会取消定义
$hash

我希望这涵盖了你的问题。只要有其他方法引用内存位置,Perl中就不会释放它。但是,如果将引用指向数据结构,则通过引用操纵该数据结构将操纵通过数组或哈希变量引用的数据结构