我在第9行的代码中创建了一个哈希的本地副本.对%d的任何更改都不会对全局%h变量(行:5)进行更改.我必须使用reference(第8行)来提供对%h的更改.
有没有办法在不创建本地副本的情况下取消引用子哈希?我问,因为我有许多参考文献的复杂记录,并且通过解引用导航它会更容易.
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5 my %h;
6 sub a {
7
8 my $href = shift;
9 my(%d) = %{$href}; # this will make a copy of global %h
10
11 $$href{1}=2; # this will make a change in global %h
12 $d{2}=2; # this will not a change in global %h
13 }
14 a(\%h);
15 print scalar (keys %h) . "\n";
Run Code Online (Sandbox Code Playgroud)
谢谢你的回复.
问题是我可以在sub中对%h进行某种"别名/绑定".我想在%d中改变%h中的%h的上下文.每当我创建%d时,他都会制作%h的本地副本 - 是否有任何方法可以避免这种情况,或者我是否必须始终使用引用?
再一次:)我知道$ href是如何工作的.我阅读了教程/手册/文档等.我没有找到答案 …
此代码有效:
my $href = shift @_; # get reference to hash
my %h = %{$href}; # dereference hash
Run Code Online (Sandbox Code Playgroud)
这个没有:
my %h = %{shift @_};
Run Code Online (Sandbox Code Playgroud)
除此之外:
my %h = ${$_[0]}
Run Code Online (Sandbox Code Playgroud)
为什么?
再精确一次:
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5 my %h;
6 sub a {
7
8 # that works - result 1
9 my $href = $_[0] || shift;
10 %h = %{$href};
11
12 # that does not work - result 0
13 # …Run Code Online (Sandbox Code Playgroud) 我正在设计通用移位算术运算符。除了按照下面介绍的方式使用 32 位多路复用器(解码器)之外,还有更好的方法来实现它吗?
ENTITY isra IS
PORT (
clk: in std_logic;
rst: in std_logic;
di: in std_logic_vector (31 downto 0);
sel: in std_logic_vector (31 downto 0);
res: out std_logic_vector (31 downto 0) := (others => '0')
);
END isra;
PROCESS
BEGIN
WAIT UNTIL clk'EVENT AND clk = '1';
IF rst = '1' THEN
res <= (others => '0');
ELSE
CASE sel IS
when X"00000001" => res <= to_stdlogicvector(to_bitvector(a) sra 1);
when X"00000002" => res <= to_stdlogicvector(to_bitvector(a) sra 2);
...
when …Run Code Online (Sandbox Code Playgroud) 我有类似的东西:
Class Foo : Base {.."my stuph" ..};
int main() {
Base *b = new Base;
Foo f (b); <== **error** "invalid conversion from Base to Foo."
..
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能将b克隆到f?
在"我的stuph"中,我有在Foo和Base之间进行锻炼的功能.
我不能将Base更改为很多,而它是由其他人编写的.
谢谢