我是Perl的非常初学者。我有一个哈希$self
,其中包含其他哈希值,特别是它包含一个键为params
的哈希值。我想更新color
params
的密钥black
对应的值,从white
更新为$self
。
换句话说,{
'params' => {
'color' => 'black',
#other keys/values here...
},
#other hashes here...
};
是如何:
{
'params' => {
'color' => 'white',
#other keys/values here...
},
#other hashes here...
};
我想要的是什么:
sub updatePrice {
my ($self, $text) = @_; # I can't change this
my %params = %{$self->{params}};
$params{color} = 'white';
print Data::Dumper::Dumper{$params{color}};
# ...
}
我尝试了什么:
Odd number of elements in anonymous hash
但我收到$VAR1 = { 'white' => undef };
警告,而打印时间为&$azCopyPath /L /Y /S /source:"$sharePath" /dest:"$destShareUri" /DestSAS:"$destShareSAS" /pattern:FileToExclude.Zip
。
我做错了什么?
答案 0 :(得分:4)
嗯,您的错误是由于您的import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDateTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) throws InterruptedException {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private JButton btn;
private StopWatch stopWatch = new StopWatch();
private Timer timer;
public TestPane() {
label = new JLabel("...");
btn = new JButton("Start");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(label, gbc);
add(btn, gbc);
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(Long.toString(stopWatch.getDuration().getSeconds()));
}
});
timer.start();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (stopWatch.isRunning()) {
stopWatch.pause();
btn.setText("Start");
} else {
stopWatch.resume();
btn.setText("Pause");
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class StopWatch {
private LocalDateTime startTime;
private Duration totalRunTime = Duration.ZERO;
public void start() {
startTime = LocalDateTime.now();
}
public void stop() {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
totalRunTime = totalRunTime.plus(runTime);
startTime = null;
}
public void pause() {
stop();
}
public void resume() {
start();
}
public void reset() {
stop();
totalRunTime = Duration.ZERO;
}
public boolean isRunning() {
return startTime != null;
}
public Duration getDuration() {
Duration currentDuration = Duration.ZERO;
currentDuration = currentDuration.plus(totalRunTime);
if (isRunning()) {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
currentDuration = currentDuration.plus(runTime);
}
return currentDuration;
}
}
}
行中{
太多Data::Dumper
造成的。
{}
也是一个匿名哈希构造函数,因此您可以:
my $hashref = { 'key' => 'value' };
在将其传递给Data::Dumper
之前,您正在做的事情只是您只提供一个值 - 这就是为什么您得到的数字是“奇数”元素"警告。 (而{ 'white' }
的哈希并不是你想要实现的目标。
尝试:
print Data::Dumper::Dumper $params{color};
或者只是:
print Data::Dumper::Dumper \%params;
但同样 - 您可能以错误的方式执行此操作 - %params
是来自$self
的树的副本,因此更新它并不会更改原始文件。
my %params = %{$self->{params}};
创建副本。
你可能想要:
$self -> {params} -> {color} = "white";
print Dumper $self;
可能值得注意 - $self
通常用于引用对象,如果您将其用于"普通&#34,则可能会感到困惑;哈希值。